1. 程式人生 > >Struts2訪問request,session,application的四種方式

Struts2訪問request,session,application的四種方式

方式1--》訪問Map型別的:

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型別的:

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


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

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;

public class 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;
}

public void setServletRequest(HttpServletRequest request) {
this.request = request;
this.session = request.getSession();
this.application = session.getServletContext();
}
}