2012-03-17 21:00:14.0|分类: struts|浏览量: 1430
You can obtain the request by asking the ActionContext or implementing ServletRequestAware. Implementing ServletRequestAware is preferred. 获得request可以通过请求ActionContext 和继承ServletRequestAware。优先 继承ServletRequestAware 1、请求ActionContext HttpServletRequest request = ServletActionContext.getRequest(); 2、继承ServletRequestAware
注: When the servlet-config Interceptor sees that an Action implements ServletRequestAware, it passes a reference to the request the Action's setServletRequest method(看看下面的源码就明白了) Servlet Config Interceptor设置action需要的所有的参数(session,Request,Response,Application) struts-default.xml中 peizhservlet-config. <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
ServletConfigInterceptor的代码如下: public class ServletConfigInterceptor extends AbstractInterceptor implements StrutsStatics { private static final long serialVersionUID = 605261777858676638L; /** * Sets action properties based on the interfaces an action implements. Things like application properties, * parameters, session attributes, etc are set based on the implementing interface. * * @param invocation an encapsulation of the action execution state. * @throws Exception if an error occurs when setting action properties. */ public String intercept(ActionInvocation invocation) throws Exception { final Object action = invocation.getAction(); final ActionContext context = invocation.getInvocationContext(); if (action instanceof ServletRequestAware) { HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); ((ServletRequestAware) action).setServletRequest(request); } if (action instanceof ServletResponseAware) { HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE); ((ServletResponseAware) action).setServletResponse(response); } if (action instanceof ParameterAware) { ((ParameterAware) action).setParameters((Map)context.getParameters()); } if (action instanceof ApplicationAware) { ((ApplicationAware) action).setApplication(context.getApplication()); } if (action instanceof SessionAware) { ((SessionAware) action).setSession(context.getSession()); } if (action instanceof RequestAware) { ((RequestAware) action).setRequest((Map) context.get("request")); } if (action instanceof PrincipalAware) { HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); if(request != null) { // We are in servtlet environment, so principal information resides in HttpServletRequest ((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request)); } } if (action instanceof ServletContextAware) { ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT); ((ServletContextAware) action).setServletContext(servletContext); } return invocation.invoke(); } }
|