手工编写代码实现对action中所有方法的输入校验
通过重写validate()方法实现,validate()方法会校验action中所有与execute方法签名相同的方法。当某个数据校验失败时,我们应该调用addFiledError()方法往系统的filedErrors添加校验失败信息(为了使用addFiled()方法,action可以继承ActionSupport),如果系统的filedErrors包含失败信息,struts2会将请求转发到名为input的result。在input视图中可以通过<s:filederror/>显示失败信息。
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
<s:fielderror></s:fielderror>
<form action="${pageContext.request.contextPath}/person/personAction_save.action" method="post">
姓名:<input type="text" name="username"><br>
手机号:<input type="text" name="mobile"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
其中:<s:fielderror></s:fielderror>显示错误的提示信息
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="person" namespace="/person" extends="struts-default"> <action name="personAction_*" class="com.siln.action.PersonAction" method="{1}"> <result name="input">/index.jsp</result> <result>/WEB-INF/page/message.jsp</result> </action> </package> </struts>
package com.siln.action;
import java.util.regex.Pattern;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class PersonAction extends ActionSupport{
private String username;
private String mobile;
public String update() {
ActionContext.getContext().put("message", "更新成功");
return "success";
}
public String save() {
ActionContext.getContext().put("message", "保存成功");
return "success";
}
@Override
public void validate() {//对action中的所有方法进行校验
if(this.username == null || "".equals(this.username)) {
this.addFieldError("username", "用户名不能为空");
}
if(this.mobile == null || "".equals(this.mobile)) {
this.addFieldError("mobile", "手机号不能为空");
}else {
if(!Pattern.compile("^1[358]\\d{9}").matcher(this.mobile).matches()) {
this.addFieldError("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;
}
}
手工编写代码实现对action中指定方法的输入校验
通过validateXxx()方法实现,validateXxx()只会校验action中方法名为xxx的方法。其中Xxx的第一个字符大写。当某个数据校验失败时,我们应该调用addFiledError()方法往系统的filedErrors添加校验失败信息(为了使用addFiled()方法,action可以继承ActionSupport),如果系统的filedErrors包含失败信息,struts2会将请求转发到名为input的result。在input视图中可以通过<s:filederror/>显示失败信息。
package com.siln.action;
import java.util.regex.Pattern;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class PersonAction extends ActionSupport{
private String username;
private String mobile;
public String update() {
ActionContext.getContext().put("message", "更新成功");
return "success";
}
public String save() {
ActionContext.getContext().put("message", "保存成功");
return "success";
}
public void validateSave() { //对action中save()进行校验
if(this.username == null || "".equals(this.username)) {
this.addFieldError("username", "用户名不能为空");
}
if(this.mobile == null || "".equals(this.mobile)) {
this.addFieldError("mobile", "手机号不能为空");
}else {
if(!Pattern.compile("^1[358]\\d{9}").matcher(this.mobile).matches()) {
this.addFieldError("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;
}
}