動(dòng)態(tài)方法調(diào)用 在Struts2中動(dòng)態(tài)方法調(diào)用有三種方式,動(dòng)態(tài)方法調(diào)用就是為了解決一個(gè)Action對(duì)應(yīng)多個(gè)請(qǐng)求的處理,以免Action太多
第一種方式:指定method屬性 這種方式我們前面已經(jīng)用到過(guò),類似下面的配置就可以實(shí)現(xiàn) <action name="chainAction" class="chapter2.action.Chapter2Action" method="chainAction"> <result name="chainAction" type="chain">redirect</result> </action> <action name="plainText" class="chapter2.action.Chapter2Action" method="plainText"> <result name="plainText" type="plainText">/WEB-INF/JspPage/chapter2/plaintext.jsp</result> </action>
第二種方式:感嘆號(hào)方式(需要開(kāi)啟),官網(wǎng)不推薦使用這種方式,建議大家不要使用. 用這種方式需要先開(kāi)啟一個(gè)開(kāi)關(guān) <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 將此常量設(shè)置為true,這種方式才能使用,使用見(jiàn)示例 Action package chapter3.action;
public class Chapter3Action { public String result1(){ return "result1"; }
public String result2(){ return "result2"; } }
Jsp中訪問(wèn)方式 <body> <a href="basePath/chapter3/chapter3Action!result1">result1</a><br><ahref="{basePath}/chapter3/chapter3Action!result2">result2</a><br> </body> 如果配置了后綴,必須這樣寫(xiě): /chapter4/chapter4Action!create.action XML中配置方式 <package name="chapter3" namespace="/chapter3" extends="struts-default"> <action name="chapter3Action" class="chapter3.action.Chapter3Action"> <result name="result1">/WEB-INF/JspPage/chapter3/result1.jsp</result> <result name="result2">/WEB-INF/JspPage/chapter3/result2.jsp</result> <result name="chapter3">/WEB-INF/JspPage/chapter3/chapter3.jsp</result> </action> </package>
第三種方式:通配符方式(官網(wǎng)推薦使用) 首先得關(guān)閉開(kāi)關(guān) <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 這一種方式是由第一種轉(zhuǎn)變過(guò)來(lái)的,我們可以看到,第一種方式有很多重復(fù)的代碼,那么我們可以進(jìn)行變形,看下面的代碼 <action name="chapter3_*" class="chapter3.action.Chapter3Action" method="{1}"> <result name="test">/…/test.jsp</result> </action> chapter3_*這里的*就是你呆會(huì)要匹配的字符串,即你在后面的請(qǐng)求中得這樣寫(xiě) http://...../ chapter3_create 或 http://...../ chapter3_update 注意,這時(shí)你action中必須有create和update方法與之匹配,甚至還可以這樣匹配 <action name="chapter3_*" class="chapter3.action.Chapter3Action" method="{1}"> <result name="test">/…/{1}.jsp</result> </action> 但是這時(shí)一定要有對(duì)應(yīng)的JSP頁(yè)面存在,并且相應(yīng)的路徑不能錯(cuò),這就對(duì)我們的命名進(jìn)行了強(qiáng)制性的規(guī)定,一定要規(guī)范.
課堂示例: Action public class Chapter4Action extends ActionSupport { public String list(){ return "list"; }
public String create(){ return "create"; }
public String index(){ return "index"; } } XML: <action name="chapter4_*" class="chapter4.action.Chapter4Action" method="{1}"> <result name="{1}">/WEB-INF/JspPage/chapter4/chapter4_{1}.jsp</result> </action>
訪問(wèn)Servlet API 有時(shí)我們需要用到Request, Response, Session,Page, ServletContext這些我們以前常用的對(duì)象,那么在Struts2中怎么樣使用到這些對(duì)象呢,通常有三種方式. 間接訪問(wèn)1 //向Session中放 ActionContext.getContext().getSession().put("wdpc", "Session中的WDPC"); //向request中放 ActionContext.getContext().put("wdpc","request中的WDPC"); //向application中放 ActionContext.getContext().getApplication().put("wdpc", "Application中的WDPC"); 取值方式: ActionContext.getContext().getSession().get("wdpc"); 間接訪問(wèn)2
Struts2中提供了一個(gè)靜態(tài)類,他里面的方法可以獲取到我們的HttpServletResponse, HttpServletRequest, 然后呢就可以還原到我們以前的使用方式了.
直接訪問(wèn) 雖然Struts2提供了ActionContext來(lái)訪問(wèn)Servlet API,但是這種方式畢竟不能直接獲取Servelt API實(shí)例,為了在Action中直接訪問(wèn)Servlet API,Struts2還提供了一系列接口 ServletContextAware 實(shí)現(xiàn)此接口后,可以取得ServletContext ServletRequestAware 實(shí)現(xiàn)此接口后,可以取得HttpServletRequest ServletResponseAware 實(shí)現(xiàn)此接口后,可以取得HttpServletResponse SessionAware 實(shí)現(xiàn)此接口后,可以取得HttpSession,注意,這里有點(diǎn)特殊,取得的是一個(gè)Map<String,Object> session,攔截器負(fù)責(zé)將session中存儲(chǔ)的鍵值進(jìn)行解析,并一一對(duì)應(yīng).
所以我們通常的做法是: public class BaseAction implements ServletResponseAware, ServletRequestAware, SessionAware {
protected HttpServletResponse response; protected HttpServletRequest request; protected Map<String, Object> session;
public void setServletResponse(HttpServletResponse response) { this.response = response; }
public void setServletRequest(HttpServletRequest request) { this.request = request; }
public void setSession(Map<String, Object> session) { this.session = session; }
public HttpServletResponse getResponse() { return response; }
public void setResponse(HttpServletResponse response) { this.response = response; }
public HttpServletRequest getRequest() { return request; }
public void setRequest(HttpServletRequest request) { this.request = request; }
public Map<String, Object> getSession() { return session; } }
為了讓BaseAction能有驗(yàn)證的功能,并且不能被實(shí)例化,開(kāi)發(fā)中我們會(huì)這樣做: public abstract class BaseAction extends ActionSupport implements ServletResponseAware, ServletRequestAware, SessionAware 然后讓我們每個(gè)模塊的Action來(lái)繼承這個(gè)BaseAction類,然后我們就可以在Action中直接使用Servelt的API了.
|