在Struts 2中从Java获取JSON对象

时间:2014-12-03 09:58:53

标签: java json struts2 actionresult struts2-json-plugin

我正在尝试使用getJSON jQuery方法从java服务器获取响应文本。虽然,当java类是简单格式(StringListMap)时,我可以得到响应数据,但在使用其他java对象时,我无法获得成功数据。

以下Java类是简单类型并获取访问成功数据和工作

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对象而不返回成功数据

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)

1 个答案:

答案 0 :(得分:1)

尝试以下代码,它应该修复错误

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 += "}";