动态参数的封装

1.动作类充当模型对象:(不推荐使用)


意义:

可以动态的接受表单中提交过来的值并为属性赋值,不需要自己单独设置request.getParameter()这些,然后在set属性这些。。。



Action类在MVC中动作类充当C层,

但是现在要把模型和动作类结合为一体,直接在动作类中定义要接受的属性,每个属性需要设置set、get、方法

并且,在表单中设置属性时他们的name要和Action类中属性的名字一样。


Action类代码:

public class PersonAction extends ActionSupport{
	
	//设置封装的属性
	private int id;
	
	private String name;
	
	private String address;
	
	private String gender;
	
	/*
	 * 下面是set 、get方法
	 */
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}


	public String hello(){
		
		//输出属性从表单中获取到的属性的值
		System.out.println(id);
		System.out.println(name);
		System.out.println(address);
		System.out.println(gender);
		
		return "success";
	}
}struts.XML配置文件代码:
<action name="hello1" class="cn.tx.action.PersonAction" method="hello">
			<result name="success">/success.jsp</result>
		</action>


jsp代码:(注意jsp中每个input的name的值和Action类中封装的属性的名相同,若不相同,框架无法自动注入)

<form action="${pageContext.request.contextPath }/hello1" method="post">
	
		ID:<input type="text" name="id"><br>
		name:<input type="text" name="name"><br>
		address:<input type="text" name="address"><br>
		gender:<input type="text" name="gender"><br>
		<input type="submit" value="tijiao">
	</form>

2.struts2动态参数封装Action和model分离封装参数(推荐使用)

动作类充当模型对象action既是C又是M,可读性很差,维护难度高,不推荐使用第一种

这种方式接收参数对提交的表单中的name有要求,name需要使用Action中model属性的名字+.

比如:person.id

Action类代码:

(动作类中需要定义model的属性,并且提供set、get方法:)

public class PersonAction1 extends ActionSupport{
	
	//封定义person对象用于接收参数
	private PersonModel person;
	
	/**
	 * 提供set。get方法
	 */
	
	public PersonModel getPerson() {
		return person;
	}
	
	public void setPerson(PersonModel person) {
		this.person = person;
	}

	
	
	public String hello(){
		System.out.println("开始打印person");
		System.out.println(person);
		return "success";
	}
}

model代码:


public class PersonModel {
	
	private int id;
	
	private String name;
	
	private String address;
	
	private String gender;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	@Override
	public String toString() {
		return "PersonModel [id=" + id + ", name=" + name + ", address="
				+ address + ", gender=" + gender + "]";
	}
	
	
}

jsp代码:

<form action="${pageContext.request.contextPath }/hello2" method="post">
	
		ID:<input type="text" name="person.id"><br>
		name:<input type="text" name="person.name"><br>
		address:<input type="text" name="person.address"><br>
		gender:<input type="text" name="person.gender"><br>
		<input type="submit" value="tijiao">
	</form>

3.struts2动态参数封装ModelDriven模型(模型驱动方式【推荐使用】)

定义

model的对象必须手动创建,否则无法注入!

第二种方式对页面上的文本域的name有要求,必须要使用model的属性名称.属性名。这样的话,页面和Action动作类有侵入性,模型驱动的方式解决了这个问题,我们要实现一个接口:ModelDriven,指定要接受的model的类型提供getModel的方法,Action类中的model对象必须要手动创建,否则无法注入属性值。


Action代码:

public class PersonAction2 extends ActionSupport implements ModelDriven<PersonModel>{
	
	//封定义person对象用于接收参数
	private PersonModel person = new PersonModel();
	
	/**
	 * 提供set。get方法
	 */
	public PersonModel getPerson() {
		return person;
	}
	
	public void setPerson(PersonModel person) {
		this.person = person;
	}

	@Override
	public PersonModel getModel() {
		return person;
	}
	
	public String hello(){
		System.out.println("开始打印person");
		System.out.println(person);
		return "success";
	}


model代码:同上(第二种)

jsp代码:

<form action="${pageContext.request.contextPath }/hello3" method="post">
	
		ID:<input type="text" name="id"><br>
		name:<input type="text" name="name"><br>
		address:<input type="text" name="address"><br>
		gender:<input type="text" name="person.gender"><br>
		<input type="submit" value="tijiao">
	</form>

这种方式的好处就是不用写

person.name

person.id

person.gender

person.address之类的东西


strust.xml配置同上



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值