Struts2中有時需要直接使用http型的幾個內置對象,就好像在JSP頁面中直接使用內置對象一樣,啟用方法可能有多種,(“啟用”一詞並非是由關到開的啟用,因為在Struts2中對內置對象做瞭一些框架上需要的封裝/包裝/處理等,所以我這裡的“啟用”更多是指“找出原始的內置對象”之意,形象比喻是有點像剝開外皮找裡面實質的東西)下面來介紹常用的三種。
一,通過 IOC 註入方式
struts2中如何啟用http型的request, response, session和application對象的方法之一IOC方式, 步驟分別如下:
1,Java文件引入對應的包
[java]
import java.util.Map; //用哪些對象引入對應的文件,
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.ServletActionContext; //struts2相關的包,其它的包根據具體情況導入
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
2,Action類中實現對應的接口
使用哪[幾]個內置對象就實現對應的哪[幾]個接口;比如:
[java]
public class RegisterAction extends ActionSupport
implements HttpServletRequest,HttpServletResponse,SessionAware,ApplicationAware{
//…other code…
}
3,在類內部定義類變量 request, response, session和 application, 實現對應接口中的setter方法
[java]
private HttpServletRequest request; //HttpServletRequest型變量request聲明
private HttpServletResponse response; //HttpServletResponse型變量response聲明
private Map session; //HttpSession型變量session聲明
private Map application; //Application型變量application聲明
public void setServletRequest(HttpServletRequest request){ //實現接口中的方法
this.request = request;
}
public void setServletResponse(HttpServletResponse response){ //實現接口中的方法
this.response = response;
}
public void setSession(Map<String, Object> session){ //實現接口中的方法
this.session = session;
}
public void setApplication(Map<String, Object> application){ //實現接口中的方法
this.application = application;
}
4,使用request,response,session和application對象
在前面工作完成之後,然後即可在execute()等執行方法中像在JSP文件中直接使用這些變量
[java]
public String execute(){
//…code…
String userName=request.getParameter("userName"); //使用
//response.sendRedirect("register/index.jsp");
session.put("userName",userName);
application.put("onlineNum",100);
//…code…
}
二,通過 ActionContext 上下文方式 [推薦]
struts2中啟用request, response, session, application對象的另外方法之一是通過ActionContext來找, 再用它們來獲取前端輸入的值, 步驟如下:
1,Java文件引入對應的包
[java]
import java.util.Map; //用哪些對象引入對應的文件
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext; //struts2相關的包,其它的包根據具體情況導入
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
2,獲取Action上下文環境
[java]
ActionContext cxt = ActionContext.getContext();
3,通過上下文環境得到對應的對象,然後強制轉換成對應類型的變量;
[java]
HttpServletRequest request = (HttpServletRequest)cxt.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse)cxt.get(ServletActionContext.HTTP_RESPONSE);
HttpSession session = (HttpSession)cxt.get(ServletActionContext.SESSION); //(HttpSession)cxt.getSession();
Application application = (Application)cxt.get(ServletActionContext.APPLICATION); //(Application)cxt.getApplication();
4,使用request, response, session和 application對象
在前面工作完成之後,然後即可在execute()等執行方法中像在JSP文件中使用這些變量,同上面IOC方式中的第4點類適的;
三,通過 ServletActionContext 方式 [推薦]
整個struts2在web.xml文件中時以 Servlet 形式展現,所以可以通過 ServletActionContext 類獲取上下文,再獲取其需要的對象
1,Java文件引入對應的包
[java]
import java.util.Map; //用哪些對象引入對應的對象
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext; //struts2相關的包,其它的包根據具體情況導入
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
2,通過 ServletActionContext 類獲取 Action 上下文環境,然後直接得到需要的對象
[java]
HttpServletRequest request = ServletActionContext.getRequest(); //直接獲得 request 對象
HttpServletResponse response = ServletActionContext.getResponse();//直接獲得response對象
HttpSession session = (HttpSession)ServletActionContext.getContext().getSession();//獲取session 對象
Map application = ServletActionContext.getContext().getApplication();//獲取application 對象
//ActionContext cxt = ActionContext.getContext(); //通過ActionContext獲取上下文
ActionContext cxt = ServletActionContext.getContext();
//通過ServletActionContext 獲取上下文,結果跟前面一行代碼一樣,隻是獲取方式不同而已
3,使用request,response,session和application對象
在前面工作完成之後,然後即可在execute()等執行方法中像在JSP文件中使用這些變量,同上面IOC方式中的第4點類適的;