摘自:http://blog.163.com/ppy2790@126/blog/static/103242241201352311433328/
1 页面向action传值: 从View层到Controller层
action添加属性,和getter , setter
(本人测试了的)
2 action向页面传值:从Controller层到View层
三种方式:
1)通过action的属性来传值
private String name;
private User user;
// getters and setters
用EL表达来取值: ${name}
通过struts2标签来取:
<
s:property
value
=
"name"
/>
(本人测试了的)
2)通过ActionContext.getContent().put(key, value);来完成值的传递
ActionContext是通过map来实现。
页面可以通过EL表达式来取值,也可以通过struts2标签来取值。
ActionContext.getContext().put("uname","Jerry");
<
s:property
value
=
"#uname"
/>
使用s:property来访问actionContext中的数据都需要加#,在struts2.3.14之后,如果数据是String类型,就不需要加#
3) 通过 ServletAPI的方式来传值
ActionContext.getRequest().setAttribute(key,value);
同样通过EL表达式也可以获取值,使用struts2标签来获取值 #request.xxx
ActionContext.getRequest().setAttribute("username","张三");
<
s:property
value
=
"#request.username"
/>
ValueStack包括两部分
ValueStackContext: 上下文(存放在Map中)
ActionContext
存在ActionContext中的对象需要用#来取出来。
ValueStackContent:
CompoundRoot -- root 是一个使用了List的栈结构,当我们调用一个Action时,首先是将Action入栈。
存在CompoundRoot中的对象不需用#取值。
在页面中加<s:debug/>显示出来。