I have following code and configuration
Pojo
public class MyInfo {
private String name;
private String desc;
//... getters, setters ...
}
My Action
package demo;
//... import statements ...
public class MyAction extends ActionSupport {
public static final String FAILURE = "failure";
private MyInfo info;
private String result;
private String message;
public String execute() {
result = SUCCESS;
return result;
}
public String processInfo() {
result = FAILURE;
try {
String name = info.getName();
//... More Statements //
result = SUCCESS;
} catch(Exeption e) {
message = "Unable to process information : " + e.getMessage;
}
return result;
}
//Getter and Setter methods of info, result, and message.
}
Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="base-ajax" namespace="/" extends="json-default,struts-default" abstract="true" >
<global-results>
<result type="json"></result>
<result name="failure" type="json"></result>
</global-results>
</package>
<package name="info-ajax" namespace="/" extends="base-ajax">
<action name="processInfo" method="processInfo" class="demo.MyAction">
<result type="json"></result>
</action>
</package>
<struts>
Rendered JSP snippet
<form id="infoForm" method="post" action="processInfo.action">
<input id="infoName" type="text" name="info.name"></input>
<input id="infoDesc" type-"text" naame="info.desc"></input>
<a id="btn-submit" href="#">Submit</a>
</form>
jQuery in the head section of JSP.
var jQ = jQuery.noConflict();
jQ(document).ready(function() {
jQ("#btn-submit").click(function() {
//perform some validation
var formData = jQ("#infoForm").serialize();
jQ.ajax({
url: "processInfo.action",
data: formData,
dataType: "json",
error: function() {
alert("Some error has occurred while processing request.");
},
success: function(response) {
if(response.result = "failure") {
alert("Information processing failed.");
} else if(response.result) {
alert("Information processed successfully.");
}
}
});
});
});
In most cases it runs smoothly. But sometimes I get NullPointerException in MyAction.processInfo() on info.getName(). It seems info is not populated. I have seen form is being submitted with proper values (used Firebug, and tamper data plugin to analyze). I don't beleive params interceptor skips creating info. There may be something missing in my configuration. Can anyone figure it out or guide me what is happening behind the scene?