Struts2_对Action中所有方法进行输入校验
2017-05-18 05:50
417 查看
手工编写代码实现对acgion中所有方法输入校验:
通过重写validate()方法实现,validate()方法会校验action中所有与execute()方法签名相同的方法。当某个数据
校验失败时,我们应该调用addFieldError()方法往系统的fieldErrors添加校验失败信息(为了使用addFieldError()方法,action
可以继承ActionSupport),如果系统的fieldError包含失败信息,struts2会将请求转发到名为input的result。在input视图中可以
通过<s:fielderror/>显示失败信息。
validate()使用例子:
@Override
public 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/>显示失败信息。
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";
}
@Override
public 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 /> 手机号:<input
type="text" name="mobile" />不能为空,并且要符合手机号的格式1,3/5/8,后面是9个数字<br /> <input
type="submit" value=" 提 交 " />
</form>
</body>
</html>
通过重写validate()方法实现,validate()方法会校验action中所有与execute()方法签名相同的方法。当某个数据
校验失败时,我们应该调用addFieldError()方法往系统的fieldErrors添加校验失败信息(为了使用addFieldError()方法,action
可以继承ActionSupport),如果系统的fieldError包含失败信息,struts2会将请求转发到名为input的result。在input视图中可以
通过<s:fielderror/>显示失败信息。
validate()使用例子:
@Override
public 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";
}
@Override
public 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 /> 手机号:<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>
相关文章推荐
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
- 转载 - Struts2基于XML配置方式实现对action的所有方法进行输入校验
- struts2对Action中所有方法进行输入校验(手工编写代码)
- 复习struts2之对action中所有方法进行输入校验
- struts2-18基于XML配置方式实现对action的所有方法或指定方法进行输入校验
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
- struts2对Action中所有的方法进行输入校验
- struts2对Action中所有的方法进行输入校验
- Struts2基于XML配置方式实现对action的所有方法进行输入校验
- struts2对action中的方法进行输入校验(2)
- Struts2_基于XML配置方式实现对action的所有方法进行校验
- 基于XML配置方式实现对action的所有方法和指定方法进行输入校验
- 复习struts2之基于XML配置方式实现对action的所有方法进行校验
- 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的所有方法进行输入校验
- Struts2:对Action中方法进行输入校验
- Struts2输入校验以及错误信息处理(1)——用Action中定义的validate()方法进行校验
- 学习笔记------------------对Action中所有方法进行输入校验和对指定方法输入校验(手工编写代码和xml方式)
- struts2中以xml配置方式对action的所有方法进行校验