Struts2 part 4:Action封装请求参数

 

Action接受页面的请求参数,有两种类型模型驱动、属性驱动,可以分为三种方式

 

1、Action自身作为model对象,使用setter封装页面参数(属性驱动)

jsp

	<form action="${pageContext.request.contextPath }/demo05/param1.action" method="post">
		文本<input type="text" name="username" /><br/>
		多选 
		 <input type="checkbox" name="programmes" value="C" />C
		 <input type="checkbox" name="programmes" value="Java" />Java
		<input type="checkbox" name="programmes" value="C++" />C++
		 <br /> 
		 <input type="submit" value="提交" />
	</form>

Action类,为Action类中的属性提供setter(),页面的属性名必须与其对应,这种方式把Action本身看作为model,但不能把Action对象传递到Service、DAO层,所有要对页面参数手动封装到JavaBean中

public class ParamAction1 extends ActionSupport {
	private String username;
	private String[] programmes;

	@Override
	public String execute() throws Exception {
		System.out.println(username);
		System.out.println(Arrays.toString(programmes));
		return NONE;
	}

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

	public void setProgrammes(String[] programmes) {
		this.programmes = programmes;
	}
}

 struts配置

	<package name="demo05" namespace="/demo05" extends="struts-default">
		<action name="param1" class="demo05.ParamAction1">
		</action>
	</package>

 

 2、创建独立的model对象(属性驱动)

jsp,参数的name属性要和action中model对象的名称对应

	<form action="${pageContext.request.contextPath }/demo05/param2.action" method="post">
		文本<input type="text" name="param.username" /><br/>
		多选 
		 <input type="checkbox" name="param.programmes" value="C" />C
		 <input type="checkbox" name="param.programmes" value="Java" />Java
		<input type="checkbox" name="param.programmes" value="C++" />C++
		 <br /> 
		 <input type="submit" value="提交" />
	</form>

 Action,为页面参数创建独立的model对象ParamBean,为其提供getter、setter属性,当封装第一参数时创建ParamBean,需要setter(),封装第二个参数是时需要getter()所以这两个方法都需要

public class ParamAction2 extends ActionSupport {
	private ParamBean param;

	@Override
	public String execute() throws Exception {
		System.out.println(param);
		return NONE;
	}

	public ParamBean getParam() {
		return this.param;
	}

	public void setParam(ParamBean param) {
		this.param = param;
	}

}

这种参数的请求封装有拦截器自动完成

<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

 model

public class ParamBean {
	private String username;
	private String[] programmes;

	public String getUsername() {
		return this.username;
	}

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

	public String[] getProgrammes() {
		return this.programmes;
	}

	public void setProgrammes(String[] programmes) {
		this.programmes = programmes;
	}

	@Override
	public String toString() {
		return "ParamBean [username=" + this.username + ", programmes=" + Arrays.toString(this.programmes) + "]";
	}
}

配置

		</action>
			<action name="param2" class="demo05.ParamAction2">
		</action>

  

3、使用ModelDriven接口(模型驱动)

 jsp,那么属性要和model中的属性对应

	<form action="${pageContext.request.contextPath }/demo05/param3.action" method="post">
		文本<input type="text" name="username" /><br/>
		多选 
		 <input type="checkbox" name="programmes" value="C" />C
		 <input type="checkbox" name="programmes" value="Java" />Java
		<input type="checkbox" name="programmes" value="C++" />C++
		 <br /> 
		 <input type="submit" value="提交" />
	</form>

 action,时间ModelDriven接口,该接口的泛型就是model对象,覆写getModel(),model对象需要在Action中手动为其初始化

public class ParamAction3 extends ActionSupport implements ModelDriven<ParamBean> {

	private ParamBean param = new ParamBean();;

	@Override
	public String execute() throws Exception {
		System.out.println(param);
		return NONE;
	}

	@Override
	public ParamBean getModel() {
		return param;
	}

}

这种方式也使用了拦截器

<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/> 

  

struts配置

	<action name="param3" class="demo05.ParamAction3">
		</action>

  

Action充当model这种方式不会使用,第二种方式更为灵活,可以在Action中创建多个model对象,ModelDriven这种形式接受页面参数时只能使用一种model对象,但可以使用针对ModeDriven的拦截器特性

 

 4、封装数据到Collection类型

jsp

<form action="${pageContext.request.contextPath }/demo05/param4.action" method="post">
		名称<input type="text" name="list[0].name" /><br/>
		价格<input type="text" name="list[0].price" /><br/>
		 <br /> 
		 名称<input type="text" name="list[1].name" /><br/>
		价格<input type="text" name="list[1].price" /><br/>
		 <input type="submit" value="提交" />
	</form>

 Action,ProductBean必须有无参构造方法,页面使用list[index].field这种方式

public class ParamAction4 extends ActionSupport {
	List<ProductBean> list;

	@Override
	public String execute() throws Exception {
		System.out.println(list);
		return NONE;
	}

	public List<ProductBean> getList() {
		return this.list;
	}

	public void setList(List<ProductBean> list) {
		this.list = list;
	}
}

 srtus配置

	<action name="param4" class="demo05.ParamAction4">
		</action>

 

 5、封装数据到Map

jsp,页面封装数据是需要设置key值,使用['key']的方式设置

	<form action="${pageContext.request.contextPath }/demo05/param5.action" method="post">
		名称<input type="text" name="map['one'].name" /><br/>
		价格<input type="text" name="map['one'].price" /><br/>
		 <br /> 
		 名称<input type="text" name="map['two'].name" /><br/>
		价格<input type="text" name="map['two'].price" /><br/>
		 <input type="submit" value="提交" />
	</form>

 Action

public class ParamAction5 extends ActionSupport {
	Map<String, ProductBean> map;

	@Override
	public String execute() throws Exception {
		System.out.println(map);
		return NONE;
	}

	public Map<String, ProductBean> getMap() {
		return this.map;
	}

	public void setMap(Map<String, ProductBean> map) {
		this.map = map;
	}

}

 配置

<action name="param5" class="demo05.ParamAction5">
		</action>

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Struts2 part 4:Action封装请求参数)