Struts2(二)获得参数的方式

访问servletAPI方式:

1.通过ActionContext

public class Demo5Action extends ActionSupport{
	public String execute()throws Exception{
		Map<String,Object> requestScope=(Map<String, Object>)ActionContext.getContext().get("request");//原生request域
		ActionContext.getContext().put("name", "requestTom");//推荐
		Map<String,Object> session=ActionContext.getContext().getSession();//获得原生session域
		session.put("name", "sessionTom");
		//application域=》map
public class Demo7Action extends ActionSupport implements ServletRequestAware{
	private HttpServletRequest request;
	public String execute()throws Exception{
	System.out.println("原生request:"+request);
	return "success";
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=request;
		
	}

Map<String,Object>applicationScope=ActionContext.getContext().getApplication();applicationScope.put("name","applicationTom");return "success";}}<body>request:{requestSoupe.name}<br/>session:{session.name}<br/>applicationScope:{applicationScope.name}<br/></body>


2.通过ServletActionContext

public String execute()throws Exception{
		//原生request
		HttpServletRequest request=ServletActionContext.getRequest();
		//原生response
		HttpServletResponse response=ServletActionContext.getResponse();
		//原生contextServlet
		 ServletContext servletContext=ServletActionContext.getServletContext();
		//原生session
		HttpSession session=request.getSession();
		return "success";
	}
3.通过实现接口
public class Demo7Action extends ActionSupport implements ServletRequestAware{
	private HttpServletRequest request;
	public String execute()throws Exception{
	System.out.println("原生request:"+request);
	return "success";
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=request;
		
	}
如何获得参数:
1.Action生命周期:
1.每次请求到来时,都会创建一个新的Action实例
2,线程安全的
2.struts2MVC
filter对应C,Action对应M,result对应V
3.属性驱动获得参数

<body>
	<form action="{pageContext.request.contextPath}/Demo8Action">
		用户名:<input type="text" name="name"/><br/>
		年龄:<input type="text" name="age"/>
		生日:<input type="text" name="birthday"/>
		<input type="submit" value="提交"/>
	</form>
</body>
public class Demo8Action extends ActionSupport{
	private String name;//准备与参数键名称相同的属性
	private Integer age;//自动类型转换,只能转换8大基本数据类型以及对应包装类
	private Date birthday;//支持特定类型字符串转换为Date如Yyyy-MM-dd
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Demo8Action(){
		System.out.println("demo8action被创建了");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String execute()throws Exception{
		System.out.println("name参数:"+name+",age参数:"+age+",生日:"+birthday);
		return "success";
	}
}
4.对象驱动
public class User {
	private String name;
	private Integer age;
	private Date birthday;
	//省略set/get方法
public class Demo9Action {
	private User user;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String execute()throws Exception{
		System.out.println(user);
		return "success";
	}
}
<form action="${pageContext.request.contextPath}/Demo9Action">
		用户名:<input type="text" name="user.name"/><br/>
		年龄:<input type="text" name="user.age"/>
		生日:<input type="text" name="user.birthday"/>
		<input type="submit" value="提交"/>
	</form>
5.模型驱动
<form action="${pageContext.request.contextPath }/Demo2Action">
		用户名:<input type="text" name="name"><br/>
		年龄:<input type="text" name="age"><br/>
		生日:<input type="text" name="birthday"><br/>
		<input type="submit" value="提交"/>
	</form>
public class Demo2Action extends ActionSupport implements ModelDriven<User>{
	private User user=new User();
	public String Execute()throws Exception{
		System.out.println(user);
		return "success";
	}
	@Override
	public User getModel() {
		return user;
	}
	//也包含一个user实体类
集合类型封装
<form action="${pageContext.request.contextPath }/Demo3Action" method="post">
		list:<input type="text" name="list"/><br/>
		list:<input type="text" name="list[1]"/><br/>
		map:<input type="text" name="map['haha']"/><br/>
		<input type="submit" value="提交"/>
	</form>
public class Demo3Action extends ActionSupport{
	private List<String>list;
	private Map<String,String> map;
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public String execute()throws Exception{
		System.out.println("list:"+list);
		System.out.println("map"+map);
		return "success";
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
}
几种转发类型:
chain:(转发到Action)用来处理Action链,被跳转的Action中仍能获取上个页面的值,
dispatcher:(转发),是默认的结果类型
redirect:(重定向),重定向到一个URL,被跳转的页面中丢失传递的信息
redirectAction(重定向到Action):重定向到一个Action
<result name="toList" type="redirectAction">
<!--重定向到的Action名字-->
<param name="actionName">CustomerAction_list</param>
<param name="namespace">/</param>
</result>
 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值