Struts2中获取JSP内置对象的方法

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

Struts2中获取HttpRequest,HttpResponse,HttpSession,Application,ServletContext及相关的Map对象。

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings( { "serial", "unused", "unchecked" })
public class Test extends ActionSupport implements ServletRequestAware, ServletResponseAware, RequestAware, SessionAware, ServletContextAware, ApplicationAware {

    private HttpServletRequest request;

    private HttpServletResponse response;

    private HttpSession session;

    private Map<String, Object> requestMap, sessionMap, contextMap;

    private ServletContext context;

    @Override
    public String execute() throws Exception {
        this.request = ServletActionContext.getRequest();
        this.request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);

        this.session = this.request.getSession();
        this.sessionMap = ActionContext.getContext().getSession();
        this.sessionMap = ServletActionContext.getContext().getSession();// 调用父类ActionContext的方法
        this.sessionMap = (Map<String, Object>) ActionContext.getContext().get(ServletActionContext.SESSION);

        this.response = ServletActionContext.getResponse();
        this.response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);

        this.context = this.session.getServletContext();
        this.context = ServletActionContext.getServletContext();
        this.context = (ServletContext) ActionContext.getContext().get(ServletActionContext.SERVLET_CONTEXT);
        this.contextMap = ActionContext.getContext().getApplication();
        this.contextMap = ActionContext.getContext().getContextMap();
        this.contextMap = (Map<String, Object>) ActionContext.getContext().get(ActionContext.APPLICATION);
        this.contextMap = (Map<String, Object>) ActionContext.getContext().get(ServletActionContext.APPLICATION);

        return SUCCESS;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    @Override
    public void setRequest(Map<String, Object> requestMap) {
        this.requestMap = requestMap;
    }

    @Override
    public void setSession(Map<String, Object> sessionMap) {
        this.sessionMap = sessionMap;
    }

    @Override
    public void setServletContext(ServletContext context) {
        this.context = context;
    }

    @Override
    public void setApplication(Map<String, Object> contextMap) {
        this.contextMap = contextMap;
    }
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:UISearchBar 搜索框

下一篇:iOS禁止全局 横屏 横屏调整视图