对action中的所有方法进行输入校验:
输入校验:
在struts2中我们可以实现对action的所有方法进行校验或者对action中的指定方法进行校验。
对于输入校验struts2提供了2种实现方法:
1、采用手工编写代码实现。
2、基于XML配置文件方式实现。
手工编写代码实现对action中所有方法的输入验证:
通过重写validate()方法实现,validate()方法会校验action中所有与execute()方法签名相同的方法。当某个数据校验失败时,应该调用addFieldError()方法往系统的filedError()方法添加校验失败信息(为了使用AddFieldError()方法,action可以实现ActionSupport),如果系统的filedErrors包含失败信息,struts会将请求转发到名为input的result。在input视图中可以通过显示失败信息。
validate()使用例子:[action中]
public void validate(){//会对action中的所有方法进行校验
if(this.mobile==null||"".equals(this.mobile.trim())){
this.addFieldError("mobile","手机号不能为空");
}else{
if(!Pattern.compile("1[358]\d{9}").matcher(this.mobile.trim()).matches()){
this.addFieldError("mobile","手机格式不正确");
}
}
}
验证失败后,请求转发至input视图:
<result name = "input" >WEN-INF/page/addUser.jsp</result>
在addUser.jsp页面使用显示失败信息。
例子:
action:
package cn.gz.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{
if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){
this.addFieldError("mobile", "手机号格式不正确");
}
}
}
}
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="person" namespace="/person" extends="struts-default">
<action name="manage_*" class="cn.gz.action.PersonAction" method="{1}">
<result name="input">/index.jsp</result>
<result name="message">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
index.jsp:
<%@ page language="java" 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="${pageContext.request.contextPath}/person/manage_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>
message.jsp:
<%@ 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>
对action中指定的方法进行输入校验:
手工编写代码实现对action中指定方法的输入校验:
通过重新validateXxx()方法实现,validateXxx()只会校验action中方法名为Xxx()的方法。其中Xxx的第一个字要大写。当某个数据校验失败时,应该调用addFieldError()方法往系统的filedError()方法添加校验失败信息(为了使用AddFieldError()方法,action可以实现ActionSupport),如果系统的filedErrors包含失败信息,struts会将请求转发到名为input的result。在input视图中可以通过显示失败信息。
validateXxx()方法使用例子:
public String add() throws Exception{
return "success";
}
public void validateAdd(){
if(username == null || "".equals(username.trim())){
this.addFieldError("username","用户名不能为空");
}
}
验证失败后,请求转发至input视图:
<result name = "input">WEB-INF/page/addUser.jsp</result>
在addUser.jsp页面用显示验证信息。
例子:
action中:
package cn.gz.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{
if(!Pattern.compile("^1[358]\\d{9}$").matcher(this.mobile).matches()){
this.addFieldError("mobile", "手机号格式不正确");
}
}
}
}
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="person" namespace="/person" extends="struts-default">
<action name="manage_*" class="cn.gz.action.PersonAction" method="{1}">
<result name="input">/index.jsp</result>
<result name="message">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
index.jsp:
<%@ page language="java" 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="${pageContext.request.contextPath}/person/manage_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>