Error Handling Issues Using Struts Health Checker

I have the following definition in

spacers-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>

      

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 show the error in the same JSP.

+2


a source to share


2 answers


After you get the ActionMessages

/ object ActionErrors

that contains the messages or errors that you want to display on your input page (using tags <html:messages>

or tags <html:errors>

), you must call one of the following methods from your object Action

to put the result of your validation into scope:

addMessages(HttpServletRequest request, ActionMessages messages)

      

or



addErrors(HttpServletRequest request, ActionMessages errors)

      

You do it?

+2


a source


I really don't care how about struts handle the exception. Using the old source code from 1.2, usually when overriding RequestProcesor

, I should probably replace two methods - process and processException

. The first thing I like is the exclusion from the request after processValidation

. The code snippet might 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 tweak the errors ahead. Forward errors are usually one of the global forwards that are easy to find from the display. Once it finds it, it likes to display your error message on the page. I think these are probably enough to handle the exception



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

      

This was done because the checkout method from ActionForm

or ValidatorForm

not throws any exceptions, and I couldn't properly override this method without throwing some. Once abandoned, who will take care of this ?!

+2


a source







All Articles