1

我在 Extjs4.1 中创建了一个简单的表单。

我将请求发送到 Struts2 框架,并使用 Strtus2JSON 插件,我必须收到响应。但不幸的是,响应为空。

动作类

public String execute()throws Exception{
    Employee emp = new Employee();
    emp.setAddress(getAddress());
    emp.setDepartment(getDepartment());
    emp.setName(getName());
    emp.setSalary(getSalary());
    AddEmployeeService empService = new AddEmployeeService();
    boolean flag = empService.addEmployee(emp);
    resultJSONobj = new JSONObject();
    if(flag == true)
        resultJSONobj.put("success","Inserted Successfully");
    else
        resultJSONobj.put("failure","An error occured");
    System.out.println(resultJSONobj);
    return SUCCESS;

}

struts.xml

<struts>
    <package name="default" extends="struts-default, json-default">
        <action name="AddEmp" class = "actions.AddEmpAction">
            <result name = "success" type="json">
                <param name ="root">resultJSONobj</param>
            </result>      
        </action>
    </package>        
</struts>

谁能告诉我为什么我得到的响应为 null ?

4

3 回答 3

1

在您的配置文件 struts.xml 中,请使用

<param name="includeProperties">resultJSONobj.*</param>

代替

<param name="includeProperties">resultJSONobj</param>

在我进行上述更改后,我在我的项目中遇到了同样的问题。

于 2013-06-06T08:40:56.687 回答
1

你有这样的 resultJSONobj 的 Getter 吗?:

public JSONObject getResultJSONobj(){ 
   return this.resultJSONobj; 
}

将命名空间添加到您的包中,如下所示:

<package name="default" namespace="/" extends="struts-default, json-default">

看看有没有什么变化……


编辑

指南中所述

  • “瞬态”字段未序列化
  • 没有 getter 方法的字段不会被序列化

试试这样:

使用 Map 而不是 JSONObject。并非每个类都被接受,而且我不确定您的 JSONObject 是否可序列化(您是否实现了可序列化?您声明了一个 serialVersionUID 吗?等)

private Map resultJSONobj = new HashMap(); //instantiated at class level

// accessors
public Map getResultJSONobj() {
    return resultJSONobj;
}
public void setResultJSONobj(Map resultJSONobj) {
    this.resultJSONobj = resultJSONobj;
}

//...

public String execute()throws Exception{
    Employee emp = new Employee();
    emp.setAddress(getAddress());
    emp.setDepartment(getDepartment());
    emp.setName(getName());
    emp.setSalary(getSalary());
    AddEmployeeService empService = new AddEmployeeService();
    boolean flag = empService.addEmployee(emp);

    if(flag == true)
        resultJSONobj.put("success","Inserted Successfully");
    else
        resultJSONobj.put("failure","An error occured");
    System.out.println(resultJSONobj);
    return SUCCESS;

}

并像这样更改 struts 配置:

<struts>
    <package name="default" extends="struts-default, json-default">

        <action name="AddEmp" class = "actions.AddEmpAction">
            <result name="success" type="json" />
        </action>
    </package>  

</struts>

成功不是必须的,但留给更清晰的代码,以后可以随时删除。

现在,您应该看到这个结果(如果您没有任何其他 getter):

{  
   "resultJSONobj": {
       "success":"Inserted Successfully"
   }
}

或者

{  
   "resultJSONobj": {
       "failure":"An error occured"
   }
}
于 2012-12-17T13:51:40.153 回答
0

struts.xml 文件中缺少一些东西。结果成功后,你要去哪里?在您的执行方法和动作类内部创建 json 对象实例。Struts2JSON 插件序列化动作类中的所有动作变量。可能是因为您只在 action 类中声明了 JSONobject 并在 execute 中实际创建了它,因此 JSON 可能会得到它 {}。

private JSONObject resultJSONobj = new JSONObject();

内部动作类和外部执行并查看,它可能会起作用。有关更多信息,您可以访问这里 http://struts.apache.org/2.2.3/docs/json-plugin.html

于 2012-12-18T06:18:08.980 回答