strust2的Action中validateXxx方法的用法

Struts2控制部分时常需要验证来自页面的信息是否合法,若在执行struts2中 public String Xxx()方法操作数据库之前需要验证,ActionSupport提供了一个很好的方法。XxxAction继承ActionSupport后重写validate()方法,如validateXxx()是在Xxx()执行之前执行。

    public String register(){
      return "register" ;
    }
    /*
     * validateXxx方法会在执行Xxx方法之前验证
     */
    public void validateRegister(){
        if(...){
          addFieldError("userName", "用户名不能为空!");
            return ;
      }
    }            

如果验证不通过,即return 了,则默认返回"input"字符串,这时需要在strust.xml中配置"input"的result,这就是为什么继承ActionSupport后strust.xml中需要配置"input" result的原因

<action name="register-*" class="registerAction" method="{1}">
        <result name="register">/index.jsp</result>
        <result name="input">/register.jsp</result>  <!-- 必须要input的result-->
</action>
原文地址:https://www.cnblogs.com/hfblogs/p/5338629.html