2

私は次のように定義しています

struts-config.xml

<struts-config>
<form-beans>
    <form-bean name="LoginForm" type="com.actionform.LoginForm"/>
      </form-beans>
      
        <action-mappings>

    <!-- action for login  -->
    <action input="/views/login.jsp" name="LoginForm" path="/Login" scope="session" type="com.actions.LoginAction"
    parameter="method" validate="true">
        <forward name="success" path="/views/Frameset.html" />
       
    </action>
       </action-mappings>

<message-resources parameter="/WEB-INF/ApplicationResources"/>
    <!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
        property="pathnames"
        value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>

ログインフォーム:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    if (userName == null || userName.length() < 1) {
        System.out.println("in validate ---");
        errors.add("userName", new ActionMessage("error.userName.required"));
        // TODO: add 'error.name.required' key to your resources
    }
    if (password == null || password.length() < 1) {
        errors.add("password", new ActionMessage("error.password.required"));
        // TODO: add 'error.name.required' key to your resources
    }
    return errors;
}

login.jsp

<html:form action="/Login?method=loginUser">

<html:errors/>

<html:text name="LoginForm" property="userName" /> 

<html:messages id="err_userName" property="userName">
            <bean:write name="err_userName" />
</html:messages>
</html:form>

プロパティファイル:

error.userName.required = User Name is required.
error.password.required = Password is required.

どこが間違っているのですか?次のエラーが発生します

javax.servlet.jsp.JspException: Cannot find bean error in any scope

同じJSPでエラーを表示したいだけです。

4

2 に答える 2

2

入力ページに表示するメッセージまたはエラーを含むActionMessages/オブジェクトを (タグまたはタグを使用して)取得したら、オブジェクトから次のいずれかのメソッドを呼び出して、検証の結果をスコープに配置する必要があります。ActionErrors<html:messages><html:errors>Action

addMessages(HttpServletRequest request, ActionMessages messages)

また

addErrors(HttpServletRequest request, ActionMessages errors)

あなたはそれをやっていますか?

于 2010-04-26T13:54:22.303 に答える
2

ストラットが例外をどのように処理するかは本当に気にしません。a をオーバーライドするときに一般的に 1.2 の古い生のコードを使用するRequestProcesorと、おそらく 2 つのメソッド ( process とprocessException. まず、リクエストが行われた後に例外をキャッチするのがうれしいprocessValidationです。コードの断片は次のようになります

Exception exception = null;
if (needValidation)
  try {
    if (! processValidate(request, response, form, mapping)) {
      return;
    }
    exception = (Exception)request.getAttribute(Globals.EXCEPTION_KEY);
  } catch (InvalidCancelException ex) {
    exception = ex;
  }
ActionForward forward;
// Check out if exception occurred

if (exception != null){
  forward = processException(request, response, exception, form, mapping);

エラーを転送するように構成した場合、2 番目の方法は非常に簡単です。通常、エラー転送は、マッピングから簡単に見つけられるグローバル転送の 1 つです。見つかったら、ページにエラー メッセージを表示します。例外を処理するにはおそらくそれらで十分だと思います

exception.printStackTrace();
log.error(exception);
request.setAttribute("error", exception.getMessage());
return mapping.findForward("error");

ActionFormまたはからのvalidateメソッドがValidatorForm例外をスローせず、例外をスローせずにこのメソッドを適切にオーバーライドできなかったため、これが行われました。一度投げられたら、誰が気にしますか?

于 2011-01-12T16:30:06.190 に答える