方式1--》访问Map类型的:
Java代码
- package cn.pdsu.edu;
- import java.util.Map;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * 通过ActionContext获得Map类型的request,session,application
- */
- publicclass ActionDemo1 extends ActionSupport {
- private Map<String,Object> request;
- private Map<String,Object> session;
- private Map<String,Object> application;
-
- public ActionDemo1() {
- this.request = (Map)ActionContext.getContext().get("request");
- this.session = ActionContext.getContext().getSession();
- this.application = ActionContext.getContext().getApplication();
- }
-
- @Override
- public String execute() throws Exception {
- request.put("message", "message");
- session.put("message", "message");
- application.put("message", "message");
- return SUCCESS;
- }
- }
package cn.pdsu.edu;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 通过ActionContext获得Map类型的request,session,application
*/
public class ActionDemo1 extends ActionSupport {
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
public ActionDemo1() {
this.request = (Map)ActionContext.getContext().get("request");
this.session = ActionContext.getContext().getSession();
this.application = ActionContext.getContext().getApplication();
}
@Override
public String execute() throws Exception {
request.put("message", "message");
session.put("message", "message");
application.put("message", "message");
return SUCCESS;
}
}
方式2--》访问map类型的:
Java代码
- package cn.pdsu.edu;
- 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.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * 通过xxxAware接口来获得Map类型的request,session,application
- */
- publicclass ActionDemo2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
- private Map<String,Object> request;
- private Map<String,Object> session;
- private Map<String,Object> application;
-
- @Override
- public String execute() throws Exception {
- request.put("message", "message");
- session.put("message", "message");
- application.put("message", "message");
- return SUCCESS;
- }
-
- publicvoid setRequest(Map<String, Object> request) {
- this.request=request;
- }
-
- publicvoid setSession(Map<String, Object> session) {
- this.session=session;
- }
-
- publicvoid setApplication(Map<String, Object> application) {
- this.application = application;
- }
- }
package cn.pdsu.edu;
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.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 通过xxxAware接口来获得Map类型的request,session,application
*/
public class ActionDemo2 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
private Map<String,Object> request;
private Map<String,Object> session;
private Map<String,Object> application;
@Override
public String execute() throws Exception {
request.put("message", "message");
session.put("message", "message");
application.put("message", "message");
return SUCCESS;
}
public void setRequest(Map<String, Object> request) {
this.request=request;
}
public void setSession(Map<String, Object> session) {
this.session=session;
}
public void setApplication(Map<String, Object> application) {
this.application = application;
}
}
方式3--》访问HttpServletRequest
Java代码
- package cn.pdsu.edu;
- import java.util.Map;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletRequest;
- 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.SessionAware;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- publicclass ActionDemo3 extends ActionSupport {
- private HttpServletRequest request;
- private HttpSession session;
- private ServletContext application;
-
- public ActionDemo3() {
- request = ServletActionContext.getRequest();
- session = request.getSession();
- application = session.getServletContext();
- }
-
- @Override
- public String execute() throws Exception {
- request.setAttribute("message", "message");
- session.setAttribute("message", "message");
- application.setAttribute("message", "message");
- return SUCCESS;
- }
- }
package cn.pdsu.edu;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
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.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ActionDemo3 extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public ActionDemo3() {
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}
@Override
public String execute() throws Exception {
request.setAttribute("message", "message");
session.setAttribute("message", "message");
application.setAttribute("message", "message");
return SUCCESS;
}
}
方式4--》通过接口访问HttpServletRequest
Java代码
- package cn.pdsu.edu;
- import java.util.Map;
- import javax.management.j2ee.statistics.ServletStats;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpServletRequest;
- 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.SessionAware;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- publicclass ActionDemo4 extends ActionSupport implements ServletRequestAware{
- private HttpServletRequest request;
- private HttpSession session;
- private ServletContext application;
-
- @Override
- public String execute() throws Exception {
- request.setAttribute("message", "message");
- session.setAttribute("message", "message");
- application.setAttribute("message", "message");
- return SUCCESS;
- }
-
- publicvoid setServletRequest(HttpServletRequest request) {
- this.request = request;
- this.session = request.getSession();
- this.application = session.getServletContext();
- }
- }