struts2对输入校验有两种方法,第一中是自己写代码实现,另一种是通过配置文件:
第一种:
校验核心代码:PersonAction.java
package chao;
import java.util.regex.Pattern;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class PersonAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String mobile;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String update() {
ActionContext.getContext().put("message", "更新成功");
return "message";
}
public String save() {
ActionContext.getContext().put("message", "保存成功");
return "message";
}
// 校验核心代码,addFieldError()方法将错误信息返回到input视图,可以在如下index.jsp中显示
// 错误提示,错误信息包括两种,一种是输入值类型转换错误,一种是不符合规范
public void validateUpdate() {
if (this.username == null || "".equals(this.username.trim())) {
this.addFieldError("username", "用户名不能为空");
}
if (this.mobile == null || "".equals(this.mobile.trim())) {
this.addFieldError("mobile", "手机号不能为空");
} else {
// 正则表达式^1[358]\\d{9}$匹配手机号1开头,第二位是3,5,8后面还有9位数字
if (!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile)
.matches()) {
this.addFieldError("mobile", "手机号格式不正确");
}
}
}
}
输入视图代码:index.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%><!-- 定义标签,显示错误信息 -->
<!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>输入校验测试</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/hello_update.action"
method="post">
用户名: <input type="text" name="username"> <br /> 手机号: <input
type="text" name="mobile"> <br /> <input type="submit"
value="提交">
</form>
<s:fielderror /><!-- 显示错误信息 -->
</body>
</html>
配置文件:struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="chao" extends="struts-default">
<action name="hello_*" class="chao.PersonAction" method="{1}">
<result name="input">/index.jsp</result>
<result name="message">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
message.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>${message }
</body>
</html>
第二种通过xml文件进行校验:
命名规则:此处代码是对action所有方法添加校验,命名规则为:ActionClassName-validation.xml,
若是对指定方法校验则为ActionClassName-ActionName(即方法名)-validation.xml 必须严格遵从。
PersonAction-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="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空</message>
</field-validator>
</field>
<field name="mobile">
<field-validator type="requiredstring"><%--字符串校验,默认去除空格 --%>
<param name="trim">true</param>
<message>手机号不能为空</message>
</field-validator>
<field-validator type="regex"><%--正则表达式校验 --%>
<param name="expression"><![CDATA[^1[358]\d{9}$]]></param>
<message>手机号格式不对头</message>
</field-validator>
</field>
</validators>
两种方法实现都很简单,在此记录一下防止忘记时找不到参考,下面列出一些其他常用校验方法:
required 必填校验器
<field-validator type="required">
<message>性别不能为空!</message>
</field-validator>
requiredstring 必填字符串校验器
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空!</message>
</field-validator>
stringlength:字符串长度校验器
<field-validator type="stringlength">
<param name="maxLength">10</param>
<param name="minLength">2</param>
<param name="trim">true</param>
<message><![CDATA[产品名称应在2-10个字符之间]]></message>
</field-validator>
email:邮件地址校验器
<field-validator type="email">
<message>电子邮件地址无效</message>
</field-validator>
regex:正则表达式校验器
<field-validator type="regex">
<param name="expression"><![CDATA[^1[358]\d{9}$]]></param>
<message>手机号格式不正确!</message>
</field-validator>
int:整数校验器
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>年龄必须在1-150之间</message>
</field-validator>
字段OGNL表达式校验器
<field name="imagefile">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[imagefile.length() <= 0]]></param>
<message>文件不能为空</message>
</field-validator>
</field>