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是否使用校验。
- Struts1的校验方式
- Struts1.1的页面校验
- Struts1.1的页面校验
- 转载 struts1的校验框架
- [struts]Struts1.1的页面校验
- struts1的验证(FormBean方式)
- struts1集成spring的方式
- XML的校验方式
- Struts的校验方式
- Struts1的两种数据校验---ActionForm校验和ValidatorForm校验
- struts1.1中validation校验框架的应用
- struts1的两种下载的方式
- Struts2的输入校验方式
- Struts集成的校验方式
- 【FAQ】表单校验的方式
- 关于struts2的校验方式
- springMVC注解方式的校验
- 【原创】Spring、Hibernate、Struts1整合的方式
- ios设计参考
- struts中取得request和response方法
- 关于viewpager中删除一个页面的方法
- CTS基础
- C语言第十一堂作业
- Struts1的校验方式
- MFC文件复制、删除
- Spring MVC学习(四)-------Controller接口控制器详解4
- 二级指针删除单向链表
- cocos2d Scene场景转换
- android JNI 多线程调用与回调
- poj 1523 spf 求割点
- andriod短信,跟拨号,运行的机制跟思考
- Struts2+Hibernate整合应用开发步骤