PS:最后附上遇到的疑惑,希望知道的人指点下
前台页面(附代码):
<%@ 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=UTF-8"> <title>Insert title here</title> </head> <body> <form action="testPeopleAction.action" method="post"> <table align="center"> <caption>类型转换的错误处理</caption> <tr> <td>用户名 <input type="text" name="name"></td> </tr> <tr> <td>密码<input type="password" name="password"></td> </tr> <tr> <td>用户年龄<input type="text" name="age"></td> </tr> <tr> <td>用户生日<input type="text" name="birth"></td> </tr> <tr align="center"> <td colspan="2"><input type="submit" value="登录"><input type="reset" value="重填"></td> </tr> </table> </form> </body> </html>
package com.hcj.action; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class TestPeopleAction extends ActionSupport { <span style="white-space:pre"> </span>private String name; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>private String password; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>private int age; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>private Date birth; <span style="white-space:pre"> </span>public String getName() { <span style="white-space:pre"> </span>return name; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setName(String name) { <span style="white-space:pre"> </span>this.name = name; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getPassword() { <span style="white-space:pre"> </span>return password; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setPassword(String password) { <span style="white-space:pre"> </span>this.password = password; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public int getAge() { <span style="white-space:pre"> </span>return age; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setAge(int age) { <span style="white-space:pre"> </span>this.age = age; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public Date getBirth() { <span style="white-space:pre"> </span>return birth; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setBirth(Date birth) { <span style="white-space:pre"> </span>this.birth = birth; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public TestPeopleAction() { <span style="white-space:pre"> </span>super(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public String execute() throws Exception { <span style="white-space:pre"> </span>// TODO Auto-generated method stub <span style="white-space:pre"> </span>return super.execute(); <span style="white-space:pre"> </span>} }在该action类同一目录下新建一个ActionName-validation.xml, 其中actionName为action类的名称,本例中则为TestPeopleAction-validation.xml。
TestPeopleAction-validation.xml内容如下(附xml文件中正则表达式不起作用的解决方案)
<?xml version="1.0" encoding="UTF-8"?> <!-- 注意DOCTYPE的地址,并不是http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd --> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <!--校验文件的根元素--> <validators> <span style="white-space:pre"> </span><!-- 校验action的name属性 --> <span style="white-space:pre"> </span><field name="name"> <span style="white-space:pre"> </span><!-- 指定用户名必须满足必填规则 --> <span style="white-space:pre"> </span><field-validator type="requiredstring"> <span style="white-space:pre"> </span><param name="trim">true</param> <span style="white-space:pre"> </span><message>必须输入姓名</message> <span style="white-space:pre"> </span></field-validator> <span style="white-space:pre"> </span><!-- 指定name属性必须匹配正则表达式 --> <span style="white-space:pre"> </span><field-validator type="regex"> <span style="white-space:pre"> </span><!-- <span style="white-space:pre"> </span>这里注意下网上大多数人给的demo里这个param里的name参数写的是expression,我调试了好久发现 <span style="white-space:pre"> </span>这样写的话这个正则表达式校验不起作用,后把param里面name参数的值改为regex就好了。 <span style="white-space:pre"> </span>PS:个人猜测可能是版本的问题,如果读者用我这个例子正则表达式不起作用的话,不妨把 <span style="white-space:pre"> </span>param里的name参数再改为expression试试(写法如下): <span style="white-space:pre"> </span><param name="expression" ><![CDATA[(\w{4,25})]]></param> <span style="white-space:pre"> </span>--> <span style="white-space:pre"> </span><param name="regex" ><![CDATA[(\w{4,25})]]></param> <span style="white-space:pre"> </span><message>姓名要在4-25位之间</message> <span style="white-space:pre"> </span></field-validator> <span style="white-space:pre"> </span></field> <span style="white-space:pre"> </span><!-- 校验action的password属性 --> <span style="white-space:pre"> </span><field name="password"> <span style="white-space:pre"> </span><!-- 指定用户名必须满足必填规则 --> <span style="white-space:pre"> </span><field-validator type="requiredstring"> <span style="white-space:pre"> </span><param name="trim">true</param> <span style="white-space:pre"> </span><message>必须输入密码</message> <span style="white-space:pre"> </span></field-validator> <span style="white-space:pre"> </span><!-- 指定name属性必须匹配正则表达式 --> <span style="white-space:pre"> </span><field-validator type="regex"> <span style="white-space:pre"> </span><param name="regex"><![CDATA[(\w{4,25})]]></param> <span style="white-space:pre"> </span><message>您输入的密码只能是字母和数字,且长度必须在4到25之间</message> <span style="white-space:pre"> </span></field-validator> <span style="white-space:pre"> </span></field> <span style="white-space:pre"> </span><!-- 指定age属性必须在指定范围内 --> <span style="white-space:pre"> </span><field name="age"> <span style="white-space:pre"> </span><field-validator type="int"> <span style="white-space:pre"> </span><param name="min">1</param> <span style="white-space:pre"> </span><param name="max">150</param> <span style="white-space:pre"> </span><message>输入年龄必须在1到150之间</message> <span style="white-space:pre"> </span></field-validator> <span style="white-space:pre"> </span></field> <span style="white-space:pre"> </span><!-- 指定birth必须在指定范围内 --> <span style="white-space:pre"> </span><field name="birth"> <span style="white-space:pre"> </span><field-validator type="date"> <span style="white-space:pre"> </span><!-- 下面指定日期字符串时,必须使用本Locale的日期格式 --> <span style="white-space:pre"> </span><param name="min">1900-01-01</param> <span style="white-space:pre"> </span><param name="max">2015-05-01</param> <span style="white-space:pre"> </span><message>生日必须在${min}到${max}之间</message> <span style="white-space:pre"> </span></field-validator> <span style="white-space:pre"> </span> <span style="white-space:pre"> </span></field> </validators>
在<field-validator>元素中添加short-circut="true"即可;
当用户验证失败后,在action中返回INPUT,因此我们需要在struts.xml中配置result name值为input的逻辑视图,并在input视图中用<s:fielderror />输出验证消息。
在struts.xml中配置result name值为input的逻辑视图:
<action name="testPeopleAction" class="com.hcj.action.TestPeopleAction"> <!--定义逻辑视图和物理资源之间的映射 --> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> <span style="color:#cc0000;"><result name="input">/input.jsp</result></span> </action>在input视图中用<s:fielderror />输出验证消息:
<%@ 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"> <!-- 导入struts2标签库 --> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>类型转换错误</h1> <!-- 输出类型转换失败提示和校验失败提示 --> <s:fielderror /> </body> </html>
实现效果:
用户输入:
响应结果:
以下遇到的问题,希望大家知道的留言给下建议,谢谢!
1.数字验证部分
<field-validator type="int"> <span style="white-space: pre; "> </span><param name="min">1</param> <span style="white-space: pre; "> </span><param name="max">150</param> <span style="white-space: pre; "> </span><message>输入年龄必须在1到150之间</message> </field-validator>
这里验证用户输入是否为1-150之间的数字,如果用户输入为空的话,也是无法通过该输入验证的。假如我数据库里age这个字段允许为空,不为空的则需要在1-150的区间里,
那么我在这个xml文件里应该怎么写?
2.日期验证部分
<field-validator type="date"> <span style="white-space: pre; "> </span><!-- 下面指定日期字符串时,必须使用本Locale的日期格式 --> <span style="white-space: pre; "> </span><param name="min">1900-01-01</param> <span style="white-space: pre; "> </span><param name="max">2015-05-01</param> <span style="white-space: pre; "> </span><message>生日必须在${min}到${max}之间</message> <span style="white-space: pre; "> </span></field-validator>这里日期验证的为1900-01-01到2015-05-01这个区间,但是用户如果输入不是xxxx-xx-xx的格式,则无法显示这个验证错误的消息。
且如果这个日期部分为非空的,那我该怎么写呢?
这里我原本准备用
<field-validator type="requiredstring"> <span style="white-space: pre; "> </span><param name="trim">true</param> <span style="white-space: pre; "> </span><message>必须输入日期</message> <span style="white-space: pre; "> </span></field-validator>
来实现非空的,可最终实现的效果就是虽然能实现非空的效果,但无法实现正常的日期验证了,例如我若输入1000-01-05的话,还是会显示必须输入日期的错误。
希望有所了解的各位在评论里给点建议哈~
struts2输入校验(附ActionName-validate.xml文件中正则表达式不起作用的解决方案),PS:有点问题希望知道的人指点下
原文地址:http://blog.csdn.net/u011768325/article/details/45331229