问题
I am trying to get response text from java server using getJSON
jQuery method. Although, I can get response data when the java class is simple format (String
, List
and Map
), I could not get success data when using other java object.
the following Java class is simple type and get access success data and work
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;
}
}
the following java class is using other java object and doesn't return success data
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;
}
}
got this error:
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)
回答1:
Try the following code, it should fix the error
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 += "}";
来源:https://stackoverflow.com/questions/27268598/getting-json-object-from-java-in-struts-2