struts2的中的验证机制

本文介绍了一个基于Struts2框架的注册Action示例,该Action实现了服务器端验证功能。通过继承ActionSupport类,利用内置的validate方法进行字段合法性检查,并在执行具体业务逻辑前确保数据的有效性。

1.服务器端验证:如果一个action类继承ActionSupport类,那么action类将会继承其validate方法,当服务器收到action请求会先执行validate方法再执行execute方法

import java.util.Date;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport{
	private	String name;
	private String pwd;
	private int age;
	private Date birthday;
	@Override
	public String execute() throws Exception {
		System.out.println("execute");
		return Action.SUCCESS;
	}
	
	public String age(){
		System.out.println(age);
		return Action.SUCCESS;
	}
	
	public void validateAge(){		
		if(age>100||age<1){
			this.addActionError("年龄不合法");
		}
	}
	
	@Override
	public void validate() {
		System.out.println("validate");
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	

	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值