Struts2_对Action中所有方法进行输入校验

来源:互联网 发布:c程序员面试题库 编辑:程序博客网 时间:2024/06/06 00:56

手工编写代码实现对acgion中所有方法输入校验:

通过重写validate()方法实现,validate()方法会校验action中所有与execute()方法签名相同的方法。当某个数据校验失败时,我们应该调用addFieldError()方法往系统的fieldErrors添加校验失败信息(为了使用addFieldError()方法,action可以继承ActionSupport),如果系统的fieldError包含失败信息,struts2会将请求转发到名为input的result。在input视图中可以通过<s:fielderror/>显示失败信息。validate()使用例子:@Overridepublic void validate() {// 会对action中的所有方法进行校验if (this.username == null || "".equals(this.username.trim())) {this.addFieldError("username", "用户名不能为空");}if (this.mobile == null || "".equals(this.mobile.trim())) {this.addFieldError("mobile", "手机号不能为空");} else {// 格式1,3/5/8,后面是9个数字if (!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()) {this.addFieldError("mobile", "手机号格式不正确");}}}验证失败后,请求转发至input视图:<result name="input">/index.jsp</result>在index.jsp页面中使用<s:fielderror/>显示失败信息。

<?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><!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker、velocity的输入 --><constant name="struts.i18n.encoding" value="UTF-8" /><!-- 该属性指定需要struts2外理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由struts2处理。 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 --><constant name="struts.action.extension" value="com,action" /><!-- 当struts的配置文件修改后,系统是否自动更新该文件,默认认值为false(生产环境下使作),开发阶断最好打开 --><constant name="struts.configuration.xml.reload" value="true" /><!-- 引用其它struts.xml配置文件 --><include file="struts_validata.xml"/></struts>

<?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="itcast" namespace="/person" extends="struts-default"><action name="list_*" class="cn.itcast.a_action.PersonAction" method="{1}"><result name="input">/index.jsp</result><result name="message">/WEB-INF/page/message.jsp</result></action></package></struts>


package cn.itcast.a_action;import java.util.regex.Pattern;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class PersonAction extends ActionSupport {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";}@Overridepublic void validate() {// 会对action中的所有方法进行校验if (this.username == null || "".equals(this.username.trim())) {this.addFieldError("username", "用户名不能为空");}if (this.mobile == null || "".equals(this.mobile.trim())) {this.addFieldError("mobile", "手机号不能为空");} else {// 格式1,3/5/8,后面是9个数字if (!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()) {this.addFieldError("mobile", "手机号格式不正确");}}}}


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><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/list_update.action"method="post">用户名:<input type="text" name="username">不能为空<br /> 手机号:<inputtype="text" name="mobile" />不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br /> <inputtype="submit" value=" 提 交 " /></form></body></html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><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>${message }</body></html>




阅读全文
0 0
原创粉丝点击