The problems in error handling using Struts validation framework

空扰寡人 提交于 2019-12-18 09:21:11

问题


I have following defined in

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>

The login form:

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>

Property file:

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

Where am I doing wrong? I am getting the following error

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

I just want to display the error in the same JSP.


回答1:


After you obtain the ActionMessages/ActionErrors object which contains the messages or errors you want to display in your input page (using <html:messages> tags or <html:errors> tags), you must call one of the following methods from your Action object to place the result of your validation in scope:

addMessages(HttpServletRequest request, ActionMessages messages)

or

addErrors(HttpServletRequest request, ActionMessages errors)

Are you doing that?




回答2:


I'm really do not care how about struts handles an exception. Using old raw code from 1.2 generally when overriding a RequestProcesor, I should probably replace the two methods - process and processException. First thing is happy about catching exception from the request after processValidation has been made. The fragment of code could look like

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);

The second is pretty easy if you have configured the errors forward. The errors forward is usually one of the global forwards that easily found from the mapping. Once it found, it likes to display your error message on the page. I think those would probably enough for processing an exception

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

It has been done because validate method from ActionForm or ValidatorForm doesn't throw any exceptions and I couldn't properly override this method without throwing some. Once thrown, who will care about it?!



来源:https://stackoverflow.com/questions/2711693/the-problems-in-error-handling-using-struts-validation-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!