次のコードと構成があります
ポジョ
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>
JSP の head セクションにある jQuery。
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
。私の設定に欠けているものがあるかもしれません。誰かがそれを理解したり、舞台裏で何が起こっているかを教えてくれますか?