2011-11-10 20 views
1

サーバー側でStruts2を使用しているプロジェクトがあり、jqGrid(JSON形式を使用)で動作させようとしています。私はいくつかのテーブルをjqGridで作成して、navGridのadd/edit/deleteボタンを使用しています。struts jqgridサーバー検証エラーメッセージ

私が持っている主な問題は、サーバー検証エラーメッセージです。私はカスタムバリデータを作成し、jspページを使って作業しています:fielderrorですが、jqGridのポップアップを追加/編集する方法を知りません。私はjqGridがクライアントに対してカスタム検証を提供することを認識していますが、これには限界があります(ユーザーの電子メールが一意であるかどうかをテストすることを考えてください。一緒にテストする必要があります。たとえば、isManagerがtrueの場合、managerCodeは空でない必要があります。逆も同様です。

クライアント検証を使用すると、エラーが発生するたびに追加/編集ウィンドウにメッセージが表示されます。どういうわけか、サーバの検証エラーメッセージを同じ方法でウィンドウに表示することはできますか?

答えて

3

私は問題を解決することができました。私は年齢のフィールドのための簡単なカスタムバリデータを使用する方法を説明します。これは従業員のために> 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に設定する必要があります。

関連する問題