私は問題を解決することができました。私は年齢のフィールドのための簡単なカスタムバリデータを使用する方法を説明します。これは従業員のために> 18でなければなりません。次にバリデーターがvalidators.xmlで宣言され、アクションにマップされ、ValidationExceptionの場合のメッセージが「従業員は18歳以上であるべきです」と想定されています。
Firebugを使用すると、フォーム内のエラー領域のIDがFormErrorであることがわかりました。サーバーからの応答を取得して処理するために、jqgridにコールバック関数errorTextFormatを構成することは可能です。 jqgrid構成では、1が
var errorFormat = function(response) {
var text = response.responseText;
$('#FormError').text(text); //sets the text in the error area to the validation //message from the server
return text;
};
で
errorTextFormat : errorFormat,
を書くことができ、問題は、サーバーが暗黙的に全体の例外スタックトレースを含む応答を送信することになりました。それに対処するために、新しい結果タイプを作成することにしました。
<package name="default" abstract="true" extends="struts-default">
...
<result-types>
<result-type name="validationError"
class="exercises.ex5.result.MyResult">
</result-type>
</result-types>
...
<action name="myaction">
...
<result name="validationException" type="validationError"></result>
<exception-mapping result="validationException"
exception="java.lang.Exception"></exception-mapping>
</action>
...
</package>
これらは私が追加/編集]ウィンドウで、検証エラーメッセージを取得するには、その後のステップがあり、今では動作します:次のように
public class MyResult implements Result {
/**
*
*/
private static final long serialVersionUID = -6814596446076941639L;
private int errorCode = 500;
public void execute(ActionInvocation invocation) throws Exception {
ActionContext actionContext = invocation.getInvocationContext();
HttpServletResponse response = (HttpServletResponse) actionContext
.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse");
Exception exception = (Exception) actionContext
.getValueStack().findValue("exception");
response.setStatus(getErrorCode());
try {
PrintWriter out = response.getWriter();
out.print(exception.getMessage());
} catch (IOException e) {
throw e;
}
}
/**
* @return the errorCode
*/
public int getErrorCode() {
return errorCode;
}
/**
* @param errorCode the errorCode to set
*/
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
}
はまた、struts.xmlに設定する必要があります。