<html:errors> struts tutorial or example

后端 未结 3 1641
终归单人心
终归单人心 2021-02-09 05:31

I\'m trying to make a login page in Struts. The idea is to validate if the user exists, etc, and then if there is an error, return to the login page with the errors in red (the

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-09 06:09

    Here's a quick summary. You have an ActionForm class, say MyForm:

    
    

    You have an Action class, say MyAction:

    
      
    
    

    "name" in the action refers to "name" in the form-bean. Because you have validate="true" your ActionForm class MyForm must define validate() method which will automatically be called:

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
      ActionErrors errors = new ActionErrors();
      if ((username==null) || (username.length() < 1)) 
          errors.add("username", new ActionError("error.username.required"));
      return errors;
    }
    

    If it returns an empty ActionErrors object, Struts goes on to call your MyAction.execute(). Otherwise, Struts displays /insert.jsp (because that's the input= parm you gave) and expands the html.errors tag to display your errors from ActionErrors.

提交回复
热议问题