Struts2的属性驱动和模型驱动

一、为什么要使用属性驱动和模型驱动?
struts2与struts很大的不同点在于,struts的execute方法提供了HttpServletRequest和  
HttpServletResponse方法在获取客户端提交的数据信息的时候需要使用HttpServletRequest的
getParameter()方法,并且还需要进行必要的数据类型转换。如何客户端提交的数据量大的时候,我们则
需要写很多的getParameter方法。这样代码量就相应的增加不少。但是struts2为我们提供了属性驱动和模
型驱动,它不需要我们写很多的获取值的方法。而只需要我们在Action中定义相应的getter方法,在界面上以

Action中的变量名作为表单元素的name属性值即可。

二、属性驱动和模型驱动有什么异同?
1.属性驱动
对于属性驱动,我们需要在Action中定义与表单元素对应的所有的属性,因而在Action中会出现很多的
getter和setter方法
2.模型驱动
对于模型驱动,使用的Action对象需要实现ModelDriven接口并给定所需要的类型.而在Action中我们只需
要定义一个封装所有数据信息的javabean
3.属性和模型驱动的相同点
当我们使用属性驱动和模型驱动的时候,必须将表单的元素中的name属性值与我们定义接收数据信息的变量
名对应起来。

三、属性驱动例子

如下面的代码所示,我们将表单中的字段都定义在Action中,这就是属性驱动

package cn.yubo.action;

import java.util.Date;

import cn.yubo.utils.TextUtils;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

	private String username;
	private String password;
	private String password2;
	private int age;
	private Date birthday;
	private Date graduate;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	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;
	}

	public Date getGraduate() {
		return graduate;
	}

	public void setGraduate(Date graduate) {
		this.graduate = graduate;
	}
	
	public String execute() throws Exception{
		return SUCCESS;
	}
	
	@Override
	public void validate() {
		super.validate();
		System.out.println("validate invoked");
		if(TextUtils.isEmpty(username)){
			addActionError("username invalidate");
		}
	}

}
模型驱动则将表单中的数据封装成javabean,并且Action类实现了ModelDriven接口,如下面的代码所示:

package com.example.action;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RegisterAction extends ActionSupport implements ModelDriven<User> {

	private User user = new User();

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		System.out.println("set user : " + user.toString());
		this.user = user;
	}

	public String execute() throws Exception {
		System.out.println("call method execute");
		return SUCCESS;
	}

	@Override
	public User getModel() {
		return user;
	}

}
这里需要注意的是,Action中定义的成员变量user需要实例化,并且getModel()方法必须返回该user对象


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yubo_725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值