struts2验证简介:基于struts2拦截器为开发者提供一套验证框架,并且可扩展,一般验证都支持
a.继承ActionSupport类
b.修改struts2.xml文件:删除method属性
c.在需要验证的Action类的相同包下,写一个"类名-validation.xml"文件
d.在"类名-validation.xml"文件加入头信息以具有提示功能
e."类名-validation.xml"文件验证代码示例
true
用户名必填
true
10
4
去掉两端空格后,字符串长度应为4-10
6
10
age必须在 ${min} 和${max}之间
年龄必须大于100
必须输入一个生日
一、内置验证
User.java
package com.java.model;
public class User {
private String userName;
private String name;
private int age;
private String email;
private String homePage;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHomePage() {
return homePage;
}
public void setHomePage(String homePage) {
this.homePage = homePage;
}
@Override
public String toString() {
return "User [userName=" + userName + ", name=" + name + ", age=" + age + ", email=" + email + ", homePage="
+ homePage + "]";
}
}
RegisterAction.java
package com.java.action;
import com.java.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println("传入的用户:"+user);
return SUCCESS;
}
}
struts.xml
/register.jsp
/success.jsp
RegisterAction-validation.xml(要写在action的包下面)
请输入用户名
6
10
用户名必须在${minLength}和${maxLength}之间
请输入姓名
18
年龄必须满18周岁
请输入邮件
邮件格式不对
请输入主页
主页格式不对
用户名和真实姓名不能相同
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
Insert title here
结果:
验证表达式:
二、自定义验证
在下载的struts2的源码包找到这个路径struts-2.3.16.3\src\apps\portlet\src\main\resources
的validators.xml这个xml文件
将他复制到项目的src下面
写自定义验证敏感词方法:
SensitiveWordValidators.java
package com.java.validators;
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
public class SensitiveWordValidators extends FieldValidatorSupport{
public void validate(Object object) throws ValidationException {
//调试发现fieldName得到"name"
String fieldName=this.getFieldName();
//获取用户输入的值
String value=this.getFieldValue(fieldName, object).toString();
if(!check(value)){
//返回错误信息给input页面
this.addFieldError(fieldName, object);
}
}
//验证方法
//返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1;
public boolean check(String value){
String sensitiveWords[]={"操","傻逼"};
for(int i=0;i-1){
return false;
}
}
return true;
}
}
在validators.xml加入刚刚写的验证方法
ValidationAction.java
package com.java.action;
import com.opensymphony.xwork2.ActionSupport;
public class ValidationAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
System.out.println("姓名:"+name);
return SUCCESS;
}
}
ValidationAction-validation.xml
请输入姓名
有敏感词汇
struts.xml
/validation.jsp
/success.jsp
视图层
validation.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
Insert title here
简单自定义验证
结果:
转载于:https://my.oschina.net/u/3848699/blog/2222133