我有以下代码和配置
波乔
public class MyInfo {
private String name;
private String desc;
//... getters, setters ...
}
我的行动
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>
呈现的 JSP 片段
<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 在 JSP 的 head 部分。
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.");
}
}
});
});
});
在大多数情况下,它运行平稳。MyAction.processInfo()
但有时我会在on中得到 NullPointerException info.getName()
。似乎info
没有人口。我已经看到表单正在以正确的值提交(使用 Firebug 和篡改数据插件进行分析)。我不相信params
拦截器会跳过创建info
。我的配置中可能缺少某些内容。谁能弄清楚或指导我幕后发生的事情?