网上关于struts2 使用Validation框架进行数据验证的例子很多 这里不一一解释每个文件每行代码是什么意思了
主要说一下自己遇到的问题,和以后要注意的情况
1.基于xml文件的配置信息,命名规范需要按照 对应“action的类名-validation”的形式,而非struts.xml中action标签的name属性值
若你的类名叫NewValidateAction,那你的验证文件文件名就是:NewValidateAction-validation.xml
2.文件名必须放在与该类同意目录下才可识别
3.NewValidateAction-validation.xml中的<field>标签,对应的是jsp中要传入的属性元素
4.<field-validator type="requiredstring">类型的意思是,现举几个常用的例子
<validators> <field name="user.userName"> <!-- 验证字符串不能为空 --> <field-validator type="requiredstring"> <!-- 去空格 --> <param name="trim">true</param> <!-- 错误提示信息 --> <message>用户名不能为空</message> </field-validator> <!-- 验证字符串长度 --> <field-validator type="stringlength"> <param name="minLength">2</param> <param name="maxLength">20</param> <message>用户名长度应在2到18个字符间</message> </field-validator> </field> <field name="user.password"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>密码不能为空</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">6</param> <param name="maxLength">18</param> <message>密码长度应在6到18个字符之间</message> </field-validator> </field> <field name="user.age"> <field-validator type="int"> <param name="min">1</param> <param name="max">150</param> <message>年龄应在1到150之间</message> </field-validator> </field> <!-- 验证字符串为日期类型 --> <field name="user.birthday"> <field-validator type="date"> <param name="min">1900-01-01</param> <param name="max">2008-10-16</param> <message>出生日期应在1900-01-01到2008-10-16</message> </field-validator> </field> </validators>
所用到的struts2.3.4的11个jar包
附源码
NewValidateAction.java
package action; import com.opensymphony.xwork2.ActionSupport; public class NewValidateAction extends ActionSupport { private String msg; // 必须输入 private int age; // 在13和20之间 public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
NewValidateAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="msg"> <field-validator type="requiredstring"> <message>请输入信息</message> </field-validator> </field> <field name="age"> <field-validator type="int"> <param name="min">13</param> <param name="max">20</param> <message>必须在13至20之间</message> </field-validator> </field> </validators>
struts.xml
<?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="demo" extends="struts-default" namespace="/test"> <action name="new_validate" class="action.NewValidateAction"> <result name="input">/validate_form.jsp</result> <result name="success">/validate_form.jsp</result> </action> </package> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
validate_form.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>验证数据</title> </head> <body> <s:form action="new_validate" namespace="/test"> <s:textfield name="msg" label="姓名" /> <s:textfield name="age" label="年龄" /> <s:submit /> </s:form> </body> </html>