基于XML配置方式实现输入校验
在JSP页面中导入<%@ taglib uri="/struts-tags" prefix="s" %>标签
在页面中显示信息用 验证的信息:<s:fielderror/>
在action中创建一个继承ActionSupport的UserInfoAction.java类
然后再action包下创建一个UserInfoAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd"> <validators> <field name="txtName"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>用户名不能为空!</message> </field-validator> </field> <field name="email"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>邮箱不能为空!</message> </field-validator> <field-validator type="email"> <message>邮箱格式不争取</message> </field-validator> </field> <field name="telePhone"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>电话不能为空</message> </field-validator> <field-validator type="regex"> <param name="expression"><![CDATA[^1[358][0-9]{9}$]]></param> <message>无效的电话</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="base" namespace="" extends="struts-default"><action name="user_*" method="{1}" class="action.UserInfoAction"><result name="ok">/index.jsp</result><result name="input">/index.jsp</result></action></package></struts>