Struts2中擷取HttpServletRequest,HttpSession等的幾種方式

來源:互聯網
上載者:User

標籤:

轉自:http://www.kaifajie.cn/struts/8944.html

package com.log;import java.io.IOException;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import org.apache.struts2.StrutsStatics;import org.apache.struts2.interceptor.RequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ognl.OgnlValueStack;/** * 1、使用Struts2 Aware攔截器 * 需要Action類實現相應的攔截器介面。如我們要獲得HttpServletResponse對象,需要實現org.apache.struts2. * interceptor.ServletResponseAware介面MyAction實現了一個ServletResponseAware介面, * 並且實現了setServletResponse方法。如果一個動作類實現了ServletResponseAware介面, * Struts2在調用execute方法之前,就會先調用setServletResponse方法,並將response參數傳入這個方法。 * 如果想獲得HttpServletRequest、HttpSession和Cookie等對象,動作類可以分別實現ServletRequestAware、 * SessionAware和CookiesAware等介面。這些介面都在org.apache.struts2.interceptor包中。 * 如果要獲得請求參數,動作類可以實現org.apache.struts2.interceptor. * ParameterAware介面,但如果只想判斷某個參數是否存在,也可以實現com.opensymphony.xwork2.interceptor. * ParameterNameAware介面。這個介面有一個acceptableParameterName方法,當Struts2獲得一個請求參數時, * 就會調用一次。讀者可以在這個方法中將所有的請求參數記錄下來,以便以後使用。這個方法的定義如下: boolean * acceptableParameterName(String parameterName) *  * @author Wei * @time 2016年10月5日 下午5:20:15 */class MyAction extends ActionSupport implements ServletResponseAware {/** *  */private static final long serialVersionUID = 1L;private javax.servlet.http.HttpServletResponse response;// 獲得HttpServletResponse對象public void setServletResponse(HttpServletResponse response) {this.response = response;}public String execute() throws Exception {response.getWriter().write("實現ServletResponseAware介面");return null;}}/* * 使用RequestAware攔截器 * 和第1種方法類似。動作類需要實現一個org.apache.struts2.interceptor.RequestAware介面。 * 所不同的是RequestAware將獲得一個com.opensymphony.xwork2.util.OgnlValueStack對象, * 這個對象可以獲得response、request及其他的一些資訊 */class FirstAction extends ActionSupport implements RequestAware {/** *  */private static final long serialVersionUID = 1L;private Map request;private HttpServletResponse response;@Overridepublic void setRequest(Map request) {this.request = request;}public String execute() throws Exception {java.util.Set<String> keys = request.keySet();// 枚舉所有的key值。實際上只有一個key:struts.valueStackfor (String key : keys)System.out.println(key);// 獲得OgnlValueStack 對象OgnlValueStack stack = (OgnlValueStack) request.get("struts.valueStack");// 獲得HttpServletResponse對象response = (HttpServletResponse) stack.getContext().get(StrutsStatics.HTTP_RESPONSE);response.getWriter().write("實現RequestAware 介面");return null;}}/** * struts2中擷取HttpServletrequest,HttpServletresponse,HttpSession的四種方式 *  * @author Wei * @time 2016年10月5日 下午5:23:02 */public class LoginAction333 {public static void main(String[] args) throws IOException {}public void method3() {/* * 3、使用ActionContext類,這個很方便 * 我們可以通過org.apache.struts2.ActionContext類的get方法獲得相應的對象。代碼如下: */HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);}public void method4() throws IOException {/* * 4、最簡單的一種,使用ServletActionContext類 * Struts2為我們提供了一種最簡單的方法獲得HttpServletResponse及其他對象。這就是org.apache.struts2 * .ServletActionContext類。我們可以直接使用ServletActionContext類的getRequest、 * getResponse方法來獲得HttpServletRequest、HttpServletResponse對象。 */HttpServletResponse response = ServletActionContext.getResponse();HttpServletRequest request = ServletActionContext.getRequest();HttpSession session = request.getSession();response.getWriter().write("hello world");}}

  

Struts2中擷取HttpServletRequest,HttpSession等的幾種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.