面向对象面向君,不负代码不负卿。 *^o^* |
在 Action 中访问 WEB 资源
1). 什么是 WEB 资源 ?
HttpServletRequest, HttpSession, ServletContext 等原生的 Servlet API。
2). 为什么访问 WEB 资源?
B\S 的应用的 Controller 中必然需要访问 WEB 资源: 向域对象中读写属性, 读写 Cookie, 获取 realPath ....
3). 如何访问 ?
I. 和 Servlet API 解耦的方式: 只能访问有限的 Servlet API 对象, 且只能访问其有限的方法(读取请求参数, 读写域对象的属性, 使 session 失效…).
注意:struts版本为2.5.13
1.使用 ActionContext
package com.struts2.actionpage;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.dispatcher.HttpParameters;
import java.util.Map;
public class TestAction {
public String execute(){
ActionContext actionContext = ActionContext.getContext() ;
Map<String,Object> applicationMap = actionContext.getApplication() ;
applicationMap.put("applicationKey","applicationValue") ;
Map<String,Object> sessionMap = actionContext.getSession() ;
sessionMap.put("sessionKey","sessionValue") ;
Map<String,Object> requestMap = (Map<String, Object>) actionContext.get("request");
requestMap.put("requestKey","requestValue");
HttpParameters paramterMap = actionContext.getParameters() ;
//System.out.println("1+1"+paramterMap.get("name"));
return "OK" ;
}
}
2.实现 XxxAware 接口
package com.struts2.awarepage;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.interceptor.*;
import java.util.Map;
public class TestAwareAction implements ApplicationAware,RequestAware,SessionAware,HttpParametersAware {
//ParameterAware弃用,推荐HttpParametersAware
private Map<String,Object> applicationMap ;
private Map<String,Object> sessionMap ;
private Map<String,Object> requestMap ;
private HttpParameters parameter ;
public String execute(){
applicationMap.put("applicationKey","applicationValue") ;
sessionMap.put("sessionKey","sessionValue") ;
requestMap.put("requestKey","requestValue") ;
System.out.println(parameter.get("name"));
return "OK" ;
}
@Override
public void setApplication(Map<String, Object> map) {
applicationMap = map ;
}
@Override
public void setRequest(Map<String, Object> map) {
requestMap = map ;
}
@Override
public void setSession(Map<String, Object> map) {
sessionMap = map ;
}
@Override
public void setParameters(HttpParameters httpParameters) {
parameter = httpParameters ;
}
}
选用的建议: 若一个 Action 类中有多个 action 方法, 且多个方法都需要使用域对象的 Map 或 parameters, 则建议使用Aware 接口的方式
session 对应的 Map 实际上是 SessionMap 类型的! 强转后若调用其 invalidate() 方法, 可以使其 session 失效!如:
((SessionMap)session).invalidate();
II. 和 Servlet API 耦合的方式: 可以访问更多的 Servlet API 对象, 且可以调用其原生的方法.
使用 ServletActionContext
package com.struts2.servletaction;
import org.apache.struts2.ServletActionContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class TestServletActionContext {
public String execute(){
HttpServletRequest request = ServletActionContext.getRequest() ;
HttpSession session = request.getSession() ;
ServletContext servletContext = ServletActionContext.getServletContext() ;
request.setAttribute("requestKey","requestValue");
session.setAttribute("sessionKey","sessionValue");
servletContext.setAttribute("applicationKey","applicationValue");
System.out.println("execute...");
return "OK" ;
}
}
实现 ServletXxxAware 接口
package com.struts2.servletawareaction;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServletContextAwareAction implements ServletContextAware,ServletResponseAware,ServletRequestAware {
//同上一个实现 XxxAware 接口
@Override
public void setServletContext(ServletContext servletContext) {
}
@Override
public void setServletRequest(HttpServletRequest httpServletRequest) {
}
@Override
public void setServletResponse(HttpServletResponse httpServletResponse) {
}
}
struts.xml配置
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="servletActionContext" extends="struts-default">
<action name="servletActionContext" class="com.struts2.servletaction.TestServletActionContext">
<result name="OK">/servletActionContext_page.jsp</result>
</action>
</package>
<package name="aware_action" extends="struts-default">
<action name="aware_page" class="com.struts2.awarepage.TestAwareAction">
<result name="OK">/aware_page.jsp</result>
</action>
</package>
<package name="action-page" extends="struts-default">
<action name="action-page" class="com.struts2.actionpage.TestAction">
<result name="OK">/action-page.jsp</result>
</action>
</package>
</struts>
jsp示例片段:
<a href="aware_page.action"> to action-page</a>
大牛,别默默看了。快登陆帮我评论吧! *^o^* |