標籤:index war xmlns 需要 span dispatch 執行 display instance
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資源