复习啊..
1
2
3
|
Map request = (Map)ActionContext.getContext().get(
"request"
);
List<Task> tasks = taskManager.findAll();
request.put(
"tasks"
, tasks);
|
1
2
3
4
5
6
7
8
9
10
|
<
s:iterator
id
=
"task"
value
=
"#request.tasks"
>
<
tr
class
=
"table_header"
>
<
td
><
s:property
value
=
"#task.tname"
/></
td
>
<
td
><
s:property
value
=
"#task.tuid"
/></
td
>
<
td
><
s:property
value
=
"#task.tstartTime"
/></
td
>
<
td
><
s:property
value
=
"#task.tendTime"
/></
td
>
<
td
><
s:property
value
=
"#task.tstate"
/></
td
>
<
td
><
input
type
=
"radio"
id
=
"choose"
name
=
"choose"
onclick
=
"getId(this.value)"
value="<s:property
value
=
'#task.tid'
/>"/></
td
>
</
tr
>
</
s:iterator
>
|
1
2
|
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute(
"username"
,
"zhangsan"
);
|
1
|
<
s:property
value
=
"#request.username"
>或者${requestScope.req}
|
1
2
3
4
5
6
7
8
9
10
|
private
HttpServletRequest request;
//实现接口中的方法
public
void
setServletRequest(HttpServletRequest request){
this
.request = request;
}
//然后在execute()方法中就可以使用了
public
String execute(){
request.setAttribute(
"username"
,
"zhangsan"
);
request.getSession().getServletContext().getApplication();
//得到Application
}
|
1
|
<
s:property
value
=
"#request.task.tname"
/>
|
在Action中设置属性,并为属性设置get/set方法。传递参数时按照属性的名字赋值即可。如xxx?name=aa
取值用request.getPrameters("属性名“);
<span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">UserAction</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> name</span><span class="pun">;</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">User</span><span class="pln"> user</span><span class="pun">;</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> userAdd</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span> <span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">user</span><span class="pun">.</span><span class="pln">getName</span><span class="pun">());</span><span class="pln"> </span> <span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="kwd">print</span><span class="pun">(</span><span class="pln">name</span><span class="pun">);</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"success"</span><span class="pun">;</span><span class="pln"> </span> <span class="pln"> </span><span class="pun">}</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> getName</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">return</span><span class="pln"> name</span><span class="pun">;</span><span class="pln"> </span> <span class="pln"> </span><span class="pun">}</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setName</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> name</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">name </span><span class="pun">=</span><span class="pln"> name</span><span class="pun">;</span><span class="pln"> </span> <span class="pln"> </span><span class="pun">}</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">User</span><span class="pln"> getUser</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">return</span><span class="pln"> user</span><span class="pun">;</span><span class="pln"> </span> <span class="pln"> </span><span class="pun">}</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setUser</span><span class="pun">(</span><span class="typ">User</span><span class="pln"> user</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span> <span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">user </span><span class="pun">=</span><span class="pln"> user</span><span class="pun">;</span><span class="pln"> </span> <span class="pln"> </span><span class="pun">}</span><span class="pln"> </span> <span class="pun">}</span><span class="pln"> </span>
注:struts2不会使用单例模式,因此每次的请求都是new 一个新对象。
2、Action 往浏览器界面传递参数:
方式有三种:
1)种:直接给Action 属性(有get/set方法)赋值 ,JSP中用
例如:
2)种:通过ActionContext传值,在Action中所调用的方法中加入:
<p><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;"><span class="typ">ActionContext</span><span class="pun">.</span><span class="pln">getContext</span><span class="pun">().</span><span class="pln">put</span><span class="pun">(</span></span><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;">"key"</span><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;"><span class="pun">,</span><span class="pln"> </span></span><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;">"value"</span><span style="font-family: Arial, Helvetica, sans-serif; line-height: 22px; white-space: normal;"><span class="pun">);</span><span class="pln"> </span></span></p>
JSP中用
3)种:通过request、session 传值。Action方法中通过取得HttpServletRequest 、HttpSession 和 Map对象设置值,
例如:
<p><span class="typ">ServletActionContext</span><span class="pun">.</span><span class="pln">getRequest</span><span class="pun">().</span><span class="pln">setAttribute</span><span class="pun">(</span><span class="str">"arg0"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">);</span><span class="pln"> </span> <span class="typ">ServletActionContext</span><span class="pun">.</span><span class="pln">getRequest</span><span class="pun">().</span><span class="pln">getSession</span><span class="pun">().</span><span class="pln">setAttribute</span><span class="pun">(</span><span class="str">"arg0"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">);</span><span class="pln"> </span> <span class="typ">ActionContext</span><span class="pun">.</span><span class="pln">getContext</span><span class="pun">().</span><span class="pln">getSession</span><span class="pun">().</span><span class="pln">put</span><span class="pun">(</span><span class="str">"key"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">);</span><span class="pln"> </span></p>
jsp页面通过:
【转自】 http://cailangwei.blog.163.com/blog/static/131458579201287112552485/
在上面的代码中, 在Action实现了一个 ServletRequestAware接口,并且实现了 setServletRequest方法。
如果一个动作类实现了 ServletRequestAware接口, Struts2在调用 execute方法之前,就会先调用setServletRequest方法,并将Request参数传入这个方法。如果想获得 HttpServletResponse、HttpSession和 Cookie等对象,动作类可以分别实现 ServletResponseAware、 SessionAware和CookiesAware等接口。这些接口都在 org.apache.struts2.interceptor包中。