Struts2中设置request session application的最常用方法

四种方式:

1. 通过ActionContext来访问request,session,application对象

2. 通过实现RequestAware、SessionAware、ApplicationAware接口来访问request,session,application对象

3. 通过ServletActionContext来访问request,session,application对象

4. 通过实现ServletRequestAware接口来访问request,session,application对象

<wbr></wbr>

第二种用的最多

<wbr></wbr>

public class UserAction2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{<wbr><wbr><br><wbr><wbr><wbr> private Map &lt;String,Object&gt; <strong>request</strong>;<wbr><wbr><br><wbr><wbr><wbr> private Map &lt;String,Object&gt; <strong>session</strong>;<wbr><wbr><br><wbr><wbr><wbr> private Map &lt;String,Object&gt; <strong>application</strong>;<wbr><wbr><br><wbr><wbr><wbr> public String execute()</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr> {<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> request.put("requestKey", "requestValue");<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> session.put("sessionKey", "sessionValue");<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> application.put("applicationKey", "applicationValue");<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> return "success";<wbr><br><wbr><wbr><wbr> }<wbr><wbr><br><wbr><wbr><wbr> public void setRequest(Map&lt;String, Object&gt; request)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr> {<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> this.request = request;<wbr><br><wbr><wbr><wbr> }<wbr><wbr><br><wbr><wbr><wbr> public void setApplication(Map&lt;String, Object&gt; application)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr> {<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> this.application = application;<wbr><br><wbr><wbr><wbr> }<wbr><wbr><br><wbr><wbr><wbr> public void setSession(Map&lt;String, Object&gt; session)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr> {<wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> this.session = session;<wbr><br><wbr><wbr><wbr> }<wbr><br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

<wbr></wbr>

前台jsp页面获取后台值的方式

<s:property value="#request.requestKey"/>|<%=request.getAttribute("requestKey")%>

<s:property value="#session.sessionKey"/>|<%=session.getAttribute("sessionKey")%>

<s:property value="#application.applicationKey"/>|<%=application.getAttribute("applicationKey")%>

<wbr></wbr>

<wbr></wbr>

关于以上四种的详细说明,见下面这篇文章

<wbr></wbr>

http://blog.csdn.net/developer_java/article/details/5882976

你可能感兴趣的:(application)