struts2验证框架

阅读更多
1. struts2验证框架

Struts2基于Struts2拦截器,为开发者提供了一套易用的验证框架,并可扩展;一般的验证都支持;

新建项目HeadFirstStruts2Chap07


2. struts2内置验证

2.1 普通字段验证;
2.2 验证表达式;



User.java

package com.andrew.model;
public class User {
    private String userName;
    private String name;
    private int age;
    private String email;
    private String homePage;
    @Override
    public String toString() {
        return "User [userName=" + userName + ", name=" + name + ", age=" + age
                + ", email=" + email + ", homePage=" + homePage + "]";
    }
    // getter and setter
}

RegisterAction.java

package com.andrew.action;
import com.andrew.model.User;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
    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;
    }
}

RegisterAction-validation.xml




    
        
            请输入用户名
        
         
             6
             10
            用户名必须在${minLength}和${maxLength}之间
        
    
    
        
            请输入姓名
        
    
    
        
            18
            年龄必须满18周岁
        
    
    
        
            请输入邮件
        
        
            邮件格式不对
        
    
    
        
            请输入主页
        
        
            主页格式不对
        
    
    
        
        用户名和真实姓名不能相同
    


struts.xml


    
        /register.jsp
        /success.jsp
    


register.jsp

<%@taglib prefix="s" uri="/struts-tags" %>

用户注册

    
    
    
    
    
    


success.jsp

注册成功

http://localhost:8080/HeadFirstStruts2Chap07/register.jsp


3. 自定义验证

ValidationAction.java

package com.andrew.action;
import com.opensymphony.xwork2.ActionSupport;
public class ValidationAction extends ActionSupport {
    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




    
        
            请输入姓名
        
        
            有敏感词汇
        
    


SensitiveWordValidators.java

package com.andrew.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 {
        String fieldName = this.getFieldName();
        String value = this.getFieldValue(fieldName, object).toString();
        if (!check(value)) {
            this.addFieldError(fieldName, object);
        }
    }
    public boolean check(String value) {
        String sensitiveWords[] = { "你", "大爷" };
        for (int i = 0; i < sensitiveWords.length; i++) {
            if (value.indexOf(sensitiveWords[i]) > -1) {
                return false;
            }
        }
        return true;
    }
}

validators.xml




    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


struts.xml


    
        /validation.jsp
        /success.jsp
    


validation.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
简单自定义验证

    
    


success.jsp

注册成功

http://localhost:8080/HeadFirstStruts2Chap07/validation.jsp

你可能感兴趣的:(Java,struts2)