Struts2验证比起Struts1验证框架来,好用多了,使程序更加清晰易读,充分利用了配置文件的作用,也算是解耦的表现吧.
核心代码如下:
1.用户注册页面register.jsp
2.注册成功欢迎页面welcome.jsp
congratulations!${user.userName}
|
3.注册处理action RegisterAction
package org.kingtoon.action;
import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.kingtoon.bean.User; import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private User user; @Override public String execute() throws Exception { if(!(user.getPassword().equals(user.getRePassword()))){ this.addFieldError("password", "请输入相同的密码"); return "input"; } else { HttpServletRequest request = ServletActionContext.getRequest (); request.setAttribute("user", user); return SUCCESS; } } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
|
4. 用户Bean User.java
package org.kingtoon.bean;
import java.util.Date;
public class User {
private String userName; private String password; private String rePassword; private Integer age; private Date birthday; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRePassword() { return rePassword; } public void setRePassword(String rePassword) { this.rePassword = rePassword; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }
} |
5.配置验证文件RegisterAction-validation.xml
< VALIDATORS>
< !-- 验证字符串不能为空 -->
< FIELD-VALIDATOR type="requiredstring">
< !-- 去空格 -->
< PARAM name="trim">true
< !-- 错误提示信息 -->
< MESSAGE>用户名不能为空
< /FIELD-VALIDATOR>
< !-- 验证字符串长度 -->
< FIELD-VALIDATOR type="stringlength">
< PARAM name="minLength">2< /PARAM>
20
< MESSAGE>用户名长度应在2到18个字符间
< FIELD-VALIDATOR type="requiredstring">
true
< MESSAGE>密码不能为空
< /FIELD-VALIDATOR>
< FIELD-VALIDATOR type="stringlength">
< PARAM name="minLength">6< /PARAM>
18
密码长度应在6到18个字符之间< /MESSAGE>
< /FIELD-VALIDATOR>
< /FIELD>
< FIELD-VALIDATOR type="int">
< PARAM name="min">1
< PARAM name="max">150
< MESSAGE>年龄应在1到150之间
< /FIELD-VALIDATOR>
< !-- 验证字符串为日期类型 -->
< FIELD-VALIDATOR type="date">
< PARAM name="min">1900-01-01
< PARAM name="max">2008-10-16< /PARAM>
< MESSAGE>出生日期应在1900-01-01到2008-10-16< /MESSAGE>
< /FIELD>
< /VALIDATORS>
6.struts2框架默认加载的配置文件struts.xml
< STRUTS> < CONSTANT name="struts.custom.i18n.resources" value="messageResource">< /CONSTANT> < RESULT name="success">/welcome.jsp< /RESULT> < RESULT name="input">/register.jsp< /RESULT> < /ACTION> < /PACKAGE> < /STRUTS>
|
7.web服务器启动时加载Struts 配置文件 web.xml
< ?XML:NAMESPACE PREFIX = [default] < web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
< filter> < filter-name>struts-cleanup org.apache.struts2.dispatcher.ActionContextCleanUp
< filter> < filter-name>struts2 org.apache.struts2.dispatcher.FilterDispatcher
< filter-mapping> < filter-name>struts-cleanup< /filter-name> /*
< filter-mapping> < filter-name>struts2< /filter-name> < url-pattern>/*< /url-pattern>
< welcome-file-list> < welcome-file>register.jsp< /welcome-file> < /welcome-file-list> < /web-app> |
至此,完毕.不过需要注意:
1.配置Struts2验证xml文档的名字有讲究:格式为:Action名字-validation.xml;
2.验证文档里的中的type类型要和VO中的User属性类型一致,否则会报类型转换错误
【编辑推荐】
- 在Eclipse中开发struts应用程序
- 手把手教你在Eclipse中配置开发Struts
- Eclipse下开发struts完整解决乱码问题
- Struts相关背景介绍
- 使用Easy Struts for Eclipse开发Struts