为幸福写歌

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Struts2中Action接收参数的方法主要有以下三种:
1.使用Action的属性接收参数:
    a.定义:在Action类中定义属性,创建get和set方法;
    b.接收:通过属性接收参数,如:userName;
    c.发送:使用属性名传递参数,如:user1!add?userName=zhangsan
2.使用DomainModel接收参数:
    a.定义:定义Model类,在Action中定义Model类的对象(不需要new),创建该对象的get和set方法;
    b.接收:通过对象的属性接收参数,如:user.getUserName();
    c.发送:使用对象的属性传递参数,如:user2!add?user.userName=zhangsan
3.使用ModelDriven接收参数:
    a.定义:Action实现ModelDriven泛型接口,定义Model类的对象(必须new),通过getModel方法返回该对象;
    b.接收:通过对象的属性接收参数,如:user.getUserName();
    c.发送:直接使用属性名传递参数,如:user2!add?userName=zhangsan;

一、通过属性接收参数

在自己的Action里面设置属性并设置set、get方法,在new Action时Struts2会自动从参数中将相应的属性设置。注意Struts2在设置属性时是调用相应的set、get方法。

例如请求的URL地址为:

http://localhost:8080/Struts2_ActionAttrParamInput/user/user!add?name=a&age=8

 

其中传递了两个参数:name和age,其值分别为:a、8,此Action执行的是add()方法
那我们只要在user这个Action对象中添加这两个成员属性并生成set、get方法

package com.chongqing.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	
	private String name;
	private int age;
	
	public String add() {
		System.out.println("name=" + name);
		System.out.println("age=" + age);
		return SUCCESS;
	}

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

  二、通过域模型(DomainModel)接收参数

域模型,实际就是先把属性封装成一个类,即域,然后作为一个属性传递给自定义的Action类

例如请求的URL地址为:

注:需要一个对象user,并且这个对象需要有两个成员属性,且具有get、set方法,然后在Action中添加一个User对象的成员属性,并且有get、set方法,就可以了。

//User类
package com.chongqing.model;

public class User {
	private String name;
	private int age;
	
	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;
	}
}

在Action中不用存多个属性,而是声明一个User对象,并设置set、get方法。Struts2会自动new User对象,而不用手工输入

//UserAction
package com.chongqing.action;

import com.chongqing.model.User;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	
	private User user;//不用手动new对象

	public String add() {
		System.out.println("name=" + user.getName());
		System.out.println("age=" + user.getAge());
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}

三、通过ModelDriven接收参数

使Action实现ModelDriven<User>这个接口,在实现接口时需要使用泛型,否则使用时需要转型,并利用其getModel()方法返回对象模型,从而获得传入的参数

例如请求的URL地址为:

参数被传入至Action后,会被ModelDriven对象根据参数名自动赋值给User对象相应的属性而生成User对象,并且由getModel()返回。那么我们在Action中就可以利用这个对象了。
注意:传入的参数名需要与对象模型中的成员属性一致

User类:

package com.chongqing.model;

public class User {
	private String name;
	private int age;
	
	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;
	}
}

UserAction:

package com.chongqing.action;

import com.chongqing.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{
	
	private User user = new User();//一定要new对象
	
	public String add() {
		System.out.println("name=" + user.getName());
		System.out.println("age=" + user.getAge());
		return SUCCESS;
	}

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

 

 

 

posted on 2017-04-12 18:05  为幸福写歌  阅读(233)  评论(0编辑  收藏  举报