2015年4月21日 星期二

[Struts2]取得 request,session,application 的方式

原始碼下載: http://goo.gl/JucNB9

主要有四種方式:

第一種 取得 Map 類型的資料,使用 ActionContext.getContext().getXXX()
第二種 取得 Map 類型的資料,使用 xxxAware
第三種 取得HtteServletRequest,HttpSession,ServletContext類型
第四種 取得HtteServletRequest,HttpSession,ServletContext類型,使用 ServletRequestAware

第一種 取得 Map 類型的資料,使用 ActionContext.getContext().getXXX()

package test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;

/**
* Created by Hsu on 2015/4/21.
*/
public class MethodOne extends ActionSupport {
private Map request;
private Map session;
private Map application;

public MethodOne(){
request = (Map) ActionContext.getContext().get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();
}


@Override
public String execute() throws Exception {
request.put("request","Method 1: I'm request");
session.put("session","Method 1: I'm session");
application.put("application","Method 1: I'm application");
return SUCCESS;
}
}



第二種 取得 Map 類型的資料,使用 xxxAware

package test;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import java.util.Map;

/**
* Created by Hsu on 2015/4/21.
*/
public class MethodTwo extends ActionSupport implements RequestAware, SessionAware, ApplicationAware {
private Map request;
private Map session;
private Map application;

@Override
public String execute() throws Exception {
request.put("request","Method 2: I'm request");
session.put("session","Method 2: I'm session");
application.put("application","Method 2: I'm application");
return SUCCESS;
}

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

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

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

}


第三種 取得HtteServletRequest,HttpSession,ServletContext類型

package test;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

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

/**
* Created by Hsu on 2015/4/21.
*/
public class MethodThree extends ActionSupport{
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;

public MethodThree(){
request = ServletActionContext.getRequest();
session = request.getSession();
application = session.getServletContext();
}


@Override
public String execute() throws Exception {
request.setAttribute("request","Method 3: I'm request");
session.setAttribute("session","Method 3: I'm session");
application.setAttribute("application","Method 3: I'm application");
return SUCCESS;
}
}


第四種 取得HtteServletRequest,HttpSession,ServletContext類型,使用 ServletRequestAware

package test;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;

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

/**
* Created by Hsu on 2015/4/21.
*/
public class MethodFour extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;

@Override
public String execute() throws Exception {
request.setAttribute("request","Method 4: I'm request");
session.setAttribute("session","Method 4: I'm session");
application.setAttribute("application","Method 4: I'm application");
return SUCCESS;
}

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

}

執行結果:


image

沒有留言 :

張貼留言