【发布时间】:2014-07-22 04:45:04
【问题描述】:
我真的在为复杂的 json 对象苦苦挣扎,如下所示:
var data= {service:{
client:{idClient: idClient},
pet:{idPet: idPet},
employee:{idEmployee: idEmployee},
conclusion: ''+conclusion}};
我通过这个发送它:
$.ajax({
url: 'registerService.action',
type:'post',
data: JSON.stringify(data),
contentType: 'application/json',
dataType: 'json',
succes: function(data){
//do something
}
});
并且触发了异常:java.lang.NullPointerException,表示action中的变量service还没有被实例化。
public class SomeAction extends ActionSupport{
private Service service; //getter y setter
public String registerService(){
Integer cod= (getServiceService().registerService(service));
return SUCCESS;
}
Service 类看起来像这样
public class Service {
//everything with its respective setter a getter
private Client client;
private Pet pet;
private Employee employee;
private String conclusion;
public Service(){
client = new Client();
pet = new Pet();
employee= new Employee();
}
}
另外,不同对象的每个id都是整数。
希望能帮到你 提前谢谢
解决方案
对象服务实例化后,它的属性为空,那是因为我没有在struts.xml中包含和映射相应拦截器的动作如下
struts.xml
<interceptors>
<interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
</interceptors>
动作映射
<action name="registerService" class="servicioActionSpring" method="registerService">
<interceptor-ref name="json"/>
<result type="json"></result>
</action>
【问题讨论】:
标签: java jquery ajax json struts2