Struts2 基于 Struts2 拦截器,为开发者提供了一套易用的验证框架,并可扩展;一般的验证都支持;
1复制HeadFirstStruts2chapter06 改名为:HeadFirstStruts2chapter07 ,web project settings,删除所有的properties文件,
2新建com.cruise.model 包,新建User类,属性userName,name age ,E-mail ,homePage
package com.cruise.model;
public class User {
private String userName;
private String name;
private int age;
private String email;
private String homePage;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHomePage() {
return homePage;
}
public void setHomePage(String homePage) {
this.homePage = homePage;
}
@Override
public String toString() {
return "User [userName=" + userName + ", name=" + name + ", age=" + age + ", email=" + email + ", homePage="
+ homePage + "]";
}
}
3新建com.eruise.action包,和RegisterAction.继承ActionSupport, user属性,execute默认方法,
package com.cruise.action;
import com.cruise.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println("打印传入的:"+user);
return SUCCESS;
}
}
4配置struts.xml文件,如果验证验证框架被拒绝,会返回一个"input",
xml version="1.0" encoding="UTF-8" ?>
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="manager" extends="struts-default">
<action name="registerAction" class="com.cruise.action.UserAction">
<result name="input">/register.jspresult>
<result name="success">/success.jspresult>
action>
package>
struts>
5配置register.jsp 注册页面,和success.jsp 文件,
register.jsp如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<s:form action="registerAction" method="post">
<s:textfield name="user.userName" label="用户名">s:textfield>
<s:textfield name="user.name" label="真实姓名">s:textfield>
<s:textfield name="user.age" label="年龄">s:textfield>
<s:textfield name="user.email" label="邮箱">s:textfield>
<s:textfield name="user.homePage" label="主页">s:textfield>
<s:submit value="提交">s:submit>
s:form>
body>
html>
success.jsp如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
注册成功!!!
table>
body>
html>
6在com.cruise.action包下,新建RegisterAction-validation.xml文件,写验证器,对各个字段进行条件验证,
xml version="1.0" encoding="UTF-8"?>
DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN""http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="user.userName">
<field-validator type="requiredstring">
<message>请输入用户名!!!message>
field-validator>
<field-validator type="stringlength">
<param name="maxLength">20param>
<param name="minLength">6param>
<message>用户名的长度必须在${minLength}和${maxLength}之间message>
field-validator>
field>
<field name="user.name">
<field-validator type="requiredstring">
<message>请输入真实姓名!!!message>
field-validator>
field>
<field name="user.age">
<field-validator type="int">
<param name="min">18param>
<message>年龄必须满18周岁!!!message>
field-validator>
field>
<field name="user.email">
<field-validator type="requiredstring">
<message>请输入邮箱!!!message>
field-validator>
<field-validator type="email">
<message>邮件格式不对!!!message>
field-validator>
field>
<field name="user.homePage">
<field-validator type="requiredstring">
<message>请输入主页!!!message>
field-validator>
<field-validator type="url">
<message>主页格式不对!!!message>
field-validator>
field>
validators>
7测试
http://localhost:8080/HeadFirstStruts2chapter07/register.jsp
8验证表达式,验证用户名和真实姓名不能相同,在RegisterAction-validation中,添加验证表达式,在register.jsp 中添加验证错误的提示代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN""http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="user.userName">
<field-validator type="requiredstring">
<message>请输入用户名!!!</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength">20</param>
<param name="minLength">6</param>
<message>用户名的长度必须在${minLength}和${maxLength}之间</message>
</field-validator>
</field>
<field name="user.name">
<field-validator type="requiredstring">
<message>请输入真实姓名!!!</message>
</field-validator>
</field>
<field name="user.age">
<field-validator type="int">
<param name="min">18</param>
<message>年龄必须满18周岁!!!</message>
</field-validator>
</field>
<field name="user.email">
<field-validator type="requiredstring">
<message>请输入邮箱!!!</message>
</field-validator>
<field-validator type="email">
<message>邮件格式不对!!!</message>
</field-validator>
</field>
<field name="user.homePage">
<field-validator type="requiredstring">
<message>请输入主页!!!</message>
</field-validator>
<field-validator type="url">
<message>主页格式不对!!!</message>
</field-validator>
</field>
<validator type="expression">
<param name="expression"><![CDATA[!user.userName.equals(user.name)]]></param>
<message>真实姓名和用户名不能相同!!!</message>
</validator>
</validators>
register.jsp文件如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:actionerror/>
注册
<s:form action="registerAction" method="post">
<s:textfield name="user.userName" label="用户名"></s:textfield>
<s:textfield name="user.name" label="真实姓名"></s:textfield>
<s:textfield name="user.age" label="年龄"></s:textfield>
<s:textfield name="user.email" label="邮箱"></s:textfield>
<s:textfield name="user.homePage" label="主页"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
9测试
http://localhost:8080/HeadFirstStruts2chapter07/register.jsp