Struts2访问web元素(包括:request,session,application和HttpServletRequest,HttpServletSession, HttpServletContext)的4种方法

说明:

1.常用的是第二中方法取到,Map类型的requestsessionapplication的引用

2.requestsessionapplication是jsp的内置对象,HttpServletRequest,HttpServletSession, HttpServletContext是servlet中与之对应的元素。

一.取得Map类型的requestsessionapplication的引用

1.  直接取得

通过ActionContext中取出相关的web元素。值栈中Stack Context实际就是ActionContext,事实上值栈也放在request里面。

 

package com.xie.action.validate;

 

import java.util.Map;

 

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

 

@SuppressWarnings("serial")

public class ActionCheck1 extends ActionSupport {

private Map req;

private Map ses;

private Map app;

public ActionCheck1(){

    req=(Map) ActionContext.getContext().get("request");

    ses=ActionContext.getContext().getSession();

    app=ActionContext.getContext().getApplication();

}

public String execute(){

    req.put("xie", "UC num is 1596144767");

    ses.put("wang", "don't know");

    app.put("liu", "try to don't know");

    return SUCCESS;

}

 

}

页面访问:    

 

      |<%=request.getAttribute("xie") %>

      |<%=session.getAttribute("wang") %>

      |<%=application.getAttribute("liu") %>

2.  通过IoC方式取得

自己定义的Action应该实现RequestAware,SessionAware,ApplicationAware接口。

 

取得web元素的代码如下:

package com.xie.action.validate;

 

import java.util.Map;

 

import org.apache.struts2.interceptor.ApplicationAware;

import org.apache.struts2.interceptor.RequestAware;

import org.apache.struts2.interceptor.SessionAware;

 

import com.opensymphony.xwork2.ActionSupport;

 

@SuppressWarnings("serial")

public class ActionCheck2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {

private Map req;

private Map ses;

private Map app;

 

public void setRequest(Map request) {

                  req=request;

}

 

public void setSession(Map session) {

    ses=session;

}

 

public void setApplication(Map application) {

    app=application;

}

public String execute(){

    req.put("xie", "UC num is 1596144767");

    ses.put("wang", "don't know");

    app.put("liu", "try to don't know");

    return SUCCESS;

}

    

}

三.取得真实类型的HttpServletRequestHttpServletSession HttpServletContext的引用

3.直接取得

代码如下:

package com.xie.action.validate;

 

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

 

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

 

@SuppressWarnings("serial")

public class ActionCheck3 extends ActionSupport {

        private HttpServletRequest req;

        private HttpSession ses;

        private ServletContext app;

        public ActionCheck3(){

          req=ServletActionContext.getRequest();

          ses=req.getSession();

          app=ses.getServletContext();

        }

    public String execute(){

         req.setAttribute("xie", "UC num is 1596144767");

         ses.setAttribute("wang", "don't know");

         app.setAttribute("liu", "try to don't know");

         return SUCCESS;

    }

}

4.通过IoC取得,需实现ServletRequestAware

取得代码如下:

package com.xie.action.validate;

 

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

 

import org.apache.struts2.interceptor.ServletRequestAware;

 

import com.opensymphony.xwork2.ActionSupport;

 

@SuppressWarnings("serial")

public class ActionCheck4 extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest req;

    private HttpSession ses;

    private ServletContext app;

 

public void setServletRequest(HttpServletRequest request) {

    req=request;

    ses=req.getSession();

    app=ses.getServletContext();

}

public String execute(){

    req.setAttribute("xie", "UC num is 1596144767");

    ses.setAttribute("wang", "don't know");

    app.setAttribute("liu", "try to don't know");

    return SUCCESS;

}

 

}

你可能感兴趣的:(NoSQL数据库)