How can we access the HttpServletRequest

You can obtain the request by asking the ActionContext or implementing ServletRequestAware. Implementing ServletRequestAware is preferred.java

得到request能夠經過請求ActionContext 和繼承ServletRequestAware。優先 繼承ServletRequestAwareapache

一、請求ActionContext session

HttpServletRequest request = ServletActionContext.getRequest();

二、繼承ServletRequestAwareapp

  • (1)Ensure that servlet-config Interceptor is included in the Action's stack.
    •  The default stack already includes servlet-config.
  • (2)Edit the Action so that it implements the ServletRequestAware interface.
    • The ServletRequestAware interface expects a setServletRequest method. You may wish to include a companion getServletRequest method

 注:    When the servlet-config Interceptor sees that an Action implements ServletRequestAware, it passes a reference to the request the Action's setServletRequest method(看看下面的源碼就明白了)ide

Servlet Config Interceptor設置action須要的全部的參數(session,Request,Response,Applicationspa

struts-default.xml中 peizhservlet-config.code

<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>

ServletConfigInterceptor的代碼以下:orm

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();
    }
}
相關文章
相關標籤/搜索