1. 在ActionForm中使用:
LoginForm.java |
public class LoginForm extends ActionForm{ private String name; private String password; /*setter and getter*/
/*validate*/ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){ ActionMessages errors = new ActionMessages(); if(this.name == null || this.name.length() < 1){ ActionMessage aError = new ActionMessage("error.required"); errors.add("userName", aError); } return errors; } } |
2. 在Action中使用:
LoginAction.java |
public class LoginAction extends Action{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ LoginForm loginForm = (LoginForm)form; String name = loginForm.getName();
if("camper".equals(name)){ ActionMessage errors = new ActionMessages(); ActionMessage aerror = new ActionError("error.invalid.userName"); errors.add(aerror); this.addError(errors); return mapping.getInputForward(); } return mapping.findMapping("result"); } } |