Struts2學習第三課 訪問Web資源
1.什麽是WEB資源?
HttpServletRequest,HttpSession,ServletContext等原生的Servlet API。
2.為什麽訪問WEB資源?
B/S的應用的Controller中必然需要訪問WEB資源,例如,向域對象中讀寫屬性,讀寫Cookie,獲取realPath等等。
3.如何訪問?
在Action中,可以通過一下方式訪問web的HttpSession,HttpServletRequest,HttpServletResponse等資源
與Servlet API解耦的訪問方式:只能訪問有限的Servlet API對象,且只能訪問其有限的方法(獲取請求參數,讀寫域對象的屬性,使session失效)
為了避免與Servlet API耦合在一起,方便Action做單元測試,struts2對HttpServletRequest,HttpSession和ServletCOntext進行封裝,構造了3個Map對象來替代這3個對象,在Action中可以直接使用HttpServletRequest,HttpServletSession,ServletContext對應的Map對象來保存和讀取數據。
通過com.opensymphony.xwork2.ActionContext
通過Action實現如下接口:
org.apache.struts2.interceptor.ApplicationAware;
org.apache.struts2.interceptor.RequestAware;
org.apache.struts2.interceptor.SessionAware;
與Servlet API耦合的訪問方式:(可以訪問更多的Servlet API對象,即可以調用其原生的方法)
通過org.apache.struts2.ServletActionContext
通過實現對應的ServletXxxAware接口
ActionContext是Action執行的上下文對象,在ActionContext中保存了Action執行所需要的所有對象,包括parameters,request,session,application等。
獲取HttpSession對應的Map等:
public Map getSession()
獲取ServletContext對應的Map對象:
public Map getApplication()
獲取請求參數對應的Map對象:
public Map getParameters()
獲取HttpServletRequest對應的Map對象:
public Object get(Object key):ActionContext類中沒有提供類似getRequest()這樣的方法來獲取HttpServletRequest對應的Map對象,要得到HttpServletRequest對應的Map對象,可以通過為get()方法傳遞"request"參數實現。
看代碼:
package logan.struts2.study; import java.util.Map; import org.apache.struts2.dispatcher.Parameter; import com.opensymphony.xwork2.ActionContext; public class TestActionContext { public String execute(){ //0.獲取ActionContext對象 //ActionContext是Action的上下文對象,可以從中獲取到當前Action需要的一切信息 ActionContext actionContext = ActionContext.getContext(); //1.獲取application對應的Map,並想其中添加一個屬性 //通過調用ActionContext 對象的getApplication()方法來獲取application對象的Map對象 Map<String, Object> applicationMap = actionContext.getApplication(); //設置屬性 applicationMap.put("applicationKey", "applicationValue"); //獲取屬性 Object date = applicationMap.get("date"); System.out.println(date); //2.session Map<String,Object> sessionMap = actionContext.getSession(); sessionMap.put("sessionKey", "sessionValue"); //3.request //ActionContext中並沒有提供getRequest方法來獲取Request對應的Map //需要手工調用get()方法,傳入request字符串來獲取。 Map<String,Object> requestMap = (Map<String, Object>) actionContext.get("request"); requestMap.put("requestKey", "requestValue"); //4.獲取請求參數對應的Map,並獲取指定的參數值 //parameters這個Map只能讀,不能寫。如果寫入,不會報錯,但是也不起作用。 Map<String,Parameter> parameters = actionContext.getParameters(); System.out.println(parameters.get("name")); return "success"; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- action VS Action類 action:代表一個Struts2的一個請求 Action類:能夠處理Struts2請求的類 --> <package name="default" namespace="/" extends="struts-default"> <action name="TestActionContext" class="logan.struts2.study.TestActionContext"> <result>/test-actionContext.jsp</result> </action> </package> </struts>
index.jsp
<%@page import="java.util.Date"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <a href="TestActionContext.action?name=logan&name=logan2">Test ActionContext</a> <% if(application.getAttribute("date") == null){ application.setAttribute("date", new Date()); } %> </body> </html>
test-actionContext.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h4>Test ActionContext Page</h4> application:${applicationScope.applicationKey } <br><br> session:${sessionScope.sessionKey } <br><br> request:${requestScope.requestKey } <br><br> <br><br> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Struts2-2</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
Struts2學習第三課 訪問Web資源