我正在尝试使用
getJSON()
jQuery 方法从 Java 服务器获取响应文本。虽然,当 Java 类具有简单格式时,我可以获取响应数据:(String
、List
和 Map
),但在使用其他 Java 对象时,我无法获取成功数据。
以下是一个 Java 类,它使用简单类型,我得到了
success
结果以及有效的数据:
package com.awitd.framework.action;
import com.opensymphony.xwork2.Action;
public class getAllJson implements Action{
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String execute() {
System.out.println(" this is from action");
data = "[";
data += "{";
data += "\"objid\":\"" + "1" + "\",";
data += "\"id\":\"" + "1" + "\",\"name\":\"" + "name" + "\"";
data += "}"; System.out.println("data " + data);
data += "]";
return SUCCESS;
}
}
以下是一个Java类,它正在使用其他Java对象,并且它不返回
success
数据:
package com.awitd.framework.action;
import java.util.List;
import com.opensymphony.xwork2.Action;
import com.awitd.framework.entity.Employee;
import com.awitd.framework.entity.Profile;
import com.awitd.framework.service.EmployeeService;
public class getAllJson implements Action{
private String data;
private EmployeeService employeeService;
private List<Employee> employeeList;
private Employee employee;
private Profile profile;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public EmployeeService getEmployeeService() {
return employeeService;
}
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
public String execute() {
System.out.println(" this is from action");
data = "[";
/*data += "{";
data += "\"objid\":\"" + "1" + "\",";
data += "\"id\":\"" + "1" + "\",\"name\":\"" + "name" + "\"";
data += "}"; System.out.println("data " + data);*/
employeeList = employeeService.getAll();
System.out.println("size........"+employeeList.size());
if (!employeeList.isEmpty()) {
for (int i=0; i<employeeList.size(); i++) {
employee = employeeList.get(i);
profile = employee.getProfile();
data += "{";
data += "\"objid\":\"" + employee.getEmployeeId() + "\",";
data += "\"id\":\"" + employee.getId() + "\",\"name\":\"" + employee.getName() + "\"";
data += ",\"dob\":\"" + profile.getDob() + "\",\"sex\":\"" + profile.getSex() + "\"";
data += ",\"email\":\"" + profile.getEmail() + "\",\"workstart\":\"" + profile.getWorkstart() + "\"";
data += ",\"study\":\"" + profile.getStudySub() + "\",\"jplevel\":\"" + profile.getJpLevel() + "\"";
data += ",\"jpgroup\":\"" + profile.getJpGroup() + "\",\"remark\":\"" + profile.getRemark() + "\"";
data += "}";
if (!(i==employeeList.size()-1))
data += ",";
}
}
data += "]";
return SUCCESS;
}
}
出现此错误:
No existing transaction found for transaction marked with propagation 'mandatory'
java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246)
org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178)
org.apache.struts2.json.JSONWriter.process(JSONWriter.java:168)
org.apache.struts2.json.JSONWriter.value(JSONWriter.java:134)
org.apache.struts2.json.JSONWriter.write(JSONWriter.java:102)
org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:116)
org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:196)
org.apache.struts2.json.JSONResult.execute(JSONResult.java:170)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:367)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:271)
尝试以下代码,它应该可以修复错误
data += "{";
data += "\"objid\":\"" + employee.getEmployeeId() + "\",";
data += "\"id\":\"" + employee.getId() + "\",\"name\":\"" + employee.getName() + "\",";
data += ",\"dob\":\"" + profile.getDob() + "\",\"sex\":\"" + profile.getSex() + "\",";
data += ",\"email\":\"" + profile.getEmail() + "\",\"workstart\":\"" + profile.getWorkstart() + "\",";
data += ",\"study\":\"" + profile.getStudySub() + "\",\"jplevel\":\"" + profile.getJpLevel() + "\",";
data += ",\"jpgroup\":\"" + profile.getJpGroup() + "\",\"remark\":\"" + profile.getRemark() + "\"";
data += "}";