Struts2_对action指定方法进行校验

47 篇文章 0 订阅

手工编写代码实现对acgion指定方法输入校验:

通过validateXxx()方法实现,validateXxx()只会校验action中方法名为Xxx的方法。其中Xxx的第一个字母要大写。当某个数据
校验失败时,我们应该调用addFieldError()方法往系统的fieldErrors添加校验失败信息(为了使用addFieldError()方法,action
可以继承ActionSupport),如果系统的fieldError包含失败信息,struts2会将请求转发到名为input的result。在input视图中可以
通过<s:fielderror/>显示失败信息。
validate()使用例子:
	public void validateUpdate() {// 会对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>


<?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";
	}

	public void validateUpdate() {// 会对update()方法校验
		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_save.action"
		method="post">
		用户名:<input type="text" name="username">不能为空<br /> 手机号:<input
			type="text" name="mobile" />不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br /> <input
			type="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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值