Struts1的校验方式

来源:互联网 发布:微电影拍摄知乎 编辑:程序博客网 时间:2024/05/21 15:43

第一类:传统的校验方式

public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        PersonForm personForm = (PersonForm)form;
        request.setAttribute("perForm", personForm);
        Map<String ,String> errors = new HashMap<String, String>();
        if(validate(errors, personForm)){
            request.setAttribute("errors", errors);
            return mapping.findForward("input");
        }
        Person person = new Person();
        BeanUtils.copyProperties(person, personForm);

        return null;
    }
   
    private boolean validate(Map<String ,String> errors , PersonForm personForm){
        if(personForm.getName() == null){
            errors.put("name", "姓名是必须的.");
        }
        if(personForm.getBirthday() == null){
            errors.put("birthday", "生日是必须的.");
        }
        return errors.size() > 0 ? true : false;
    }

jsp 中的取值

  <body>
    <p>${errors}</p>    
    <form action="${ctx}/person/save.do" method="post">
      <p>姓名:<input type="text" name="name" value="${perForm.name}"/> ${errors["name"]} </p>
      <p>性别:<select name="gender">
            <option value="1" ${perForm.gender == 1 ? "selected=\"selected\"" : ""}>男</option>
            <option value="2" ${perForm.gender == 2 ? "selected=\"    selected\"" : ""}>女</option>
         </select>
      </p>
      <p>体重:<input type="text" name="weight" value="${perForm.weight}"/></p>
      <p>生日:<input type="text" name="birthday" value="${perForm.birthday}"/> ${errors["birthday"]}</p>
      <p><input type="submit" value="Submit"/></p>
    </form>
  </body>

配置文件中

<action path="/person/save" name="personForm"
            type="org.itfuture.examples.web.struts.action.PersonSaveAction">
            <forward name="input" path="/person/saveForm.do"/>
        </action>

第二类 :struts1 中的校验方式


public class PersonForm extends ActionForm {
    private static final long serialVersionUID = 4583316882219621916L;
    private String name;
    private Integer gender;
    private BigDecimal weight;
    private String birthday;
    private Integer[] checkedIds;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getGender() {
        return gender;
    }
    public void setGender(Integer gender) {
        this.gender = gender;
    }
    public BigDecimal getWeight() {
        return weight;
    }
    public void setWeight(BigDecimal weight) {
        this.weight = weight;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    public Integer[] getCheckedIds() {
        return checkedIds;
    }
    public void setCheckedIds(Integer[] checkedIds) {
        this.checkedIds = checkedIds;
    }
    @Override
    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
        System.out.println("PersonForm.validate()");
        ActionErrors errors = new ActionErrors();
        if(name == null) {
            errors.add("name" , new ActionMessage("姓名是必须的." , false));
        }
        if(birthday == null){
            errors.add("birthday" , new ActionMessage("生日是必须的." , false));
        }
        System.out.println(errors);
        //Globals.ERROR_KEY = "org.apache.struts.action.ERROR"
        //request.setAttribute(Globals.ERROR_KEY , errors);
        return errors;
    }
  
}


jsp 中

<body>
    <p>${requestScope["org.apache.struts.action.ERROR"]}</p>    
    <form action="${ctx}/person/save.do" method="post">
      <p>姓名:<input type="text" name="name" value="${requestScope.perForm.name}"/>  </p>
      <p>性别:<select name="gender">
            <option value="1" >男</option>
            <option value="2" >女</option>
         </select>
      </p>
      <p>体重:<input type="text" name="weight" value="${requestScope.perForm.weight}"/></p>
      <p>生日:<input type="text" name="birthday" value="${requestScope.perForm.weight}"/> </p>
      <p><input type="submit" value="Submit"/></p>
    </form>
  </body>

配置文件中

<action path="/person/save" name="personForm" input="/person/saveForm.do" scope="request" attribute="perForm" validate="true"
            type="org.itfuture.examples.web.struts.action.PersonSaveAction">
 </action>

validate="true"  由于多个action 会同时用一个FormBean 但不是每一个都需要进行数据验证 所以action 中的validate="true" 是指定该action是否使用校验。

原创粉丝点击