struts2中以xml配置方式对action的所有方法进行校验
2012-07-24 21:42
169 查看
xml文件中的<!CDATA[[]]>问题:
为了避免输入的文本与xml文件中某些特殊字符相冲突可以用<!CDATA[[]]>将文本扣起来,表示此文本是纯文本。如正则表达式<!CDATA[[^1[358]\d{9}$]]>。
案例:
DataValidateAction.java:
package blog.validate; import java.util.Date; import java.util.regex.Pattern; import com.opensymphony.xwork2.ActionSupport; public class DataValidateAction extends ActionSupport { private static final long serialVersionUID = -1817223356525882414L; private String user; private String mobile; private Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String execute(){ return "success"; } public String save(){ return "success"; } }
DataValidateAction-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.2.dtd"> <validators> <field name="user"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>用户名不能为空!</message> </field-validator> </field> <field name="mobile"> <field-validator type="requiredstring"> <message>手机号码不能为空!</message> </field-validator> <field-validator type="regex"> <param name="expression"><![CDATA[^1[358]\d{9}$]]></param> <message>手机号码格式不正确</message> </field-validator> </field> </validators>
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>校验输入</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> </head> <body> <s:fielderror/> <form action="<%=request.getContextPath() %>/person/validate_execute.action" method="post" > 用户姓名:<input type="text" name="user"/>不能为空<br/> 手机号码:<input type="text" name="mobile"/>不能为空,并且要符合规则<br/> <input type="submit" value="提交"/> </form> </body> </html>
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="validate" extends="struts-default" namespace="/person"> <action name="validate_*" class="blog.validate.DataValidateAction" method="{1}"> <result name="input">/index.jsp</result> <result name="success">/index.jsp</result> </action> </package> </struts>
相关文章推荐
- Struts2基于XML配置方式实现对action的所有方法进行输入校验
- 复习struts2之基于XML配置方式实现对action的所有方法进行校验
- Struts2 基于XML配置方式实现对action的所有方法进行校验
- 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验
- struts2中以xml配置方式对action的所有方法进行校验
- struts2-18基于XML配置方式实现对action的所有方法或指定方法进行输入校验
- Struts2_基于XML配置方式实现对action的所有方法进行校验
- 基于XML配置方式实现对action的所有方法和指定方法进行输入校验
- 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的所有方法进行输入校验
- struts2对action中的方法进行输入校验---xml配置方式(3)
- struts2基于xml配置方式对Action方法进行校验
- Struts2学习笔记(十四):基于XML配置方式实现对action的所有方法进行校验
- 基于XML配置方式实现对action的所有方法进行校验
- Struts2基于XML配置方式实现对Action方法进行校验
- 基于XML配置方式实现对action的所有方法进行输入校验
- 基于XML配置方式实现对action的所有方法进行校验
- 第二十二章 基于XML配置方式实现对action的所有方法进行校验
- struts2基于XML配置方式对指定action方法进行校验
- xml配置方式实现action的所有方法的输入校验
- Struts2_基于XML配置方式对指定action方法实现输入校验、基于XML校验的一些特点