小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Struts2動(dòng)態(tài)方法調(diào)用

 KhanLau 2015-11-18

動(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)用到過,類似下面的配置就可以實(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)方式(需要開啟),官網(wǎng)不推薦使用這種方式,建議大家不要使用.
用這種方式需要先開啟一個(gè)開關(guān)
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
將此常量設(shè)置為true,這種方式才能使用,使用見示例
Action
package chapter3.action;

public class Chapter3Action {
public String result1(){
   return "result1";
}

public String result2(){
   return "result2";
}
}

Jsp中訪問方式
<body>
    <a href="basePath/chapter3/chapter3Action!result1">result1</a><br><ahref="{basePath}/chapter3/chapter3Action!result2">result2</a><br>
</body>
如果配置了后綴,必須這樣寫:
/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)閉開關(guān)
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
這一種方式是由第一種轉(zhuǎn)變過來的,我們可以看到,第一種方式有很多重復(fù)的代碼,那么我們可以進(jìn)行變形,看下面的代碼
<action name="chapter3_*" class="chapter3.action.Chapter3Action"
method="{1}">
<result name="test">/…/test.jsp</result>
</action>
chapter3_*這里的*就是你呆會(huì)要匹配的字符串,即你在后面的請(qǐng)求中得這樣寫
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ī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>


訪問Servlet API
有時(shí)我們需要用到Request, Response, Session,Page, ServletContext這些我們以前常用的對(duì)象,那么在Struts2中怎么樣使用到這些對(duì)象呢,通常有三種方式.
間接訪問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");
間接訪問2


Struts2中提供了一個(gè)靜態(tài)類,他里面的方法可以獲取到我們的HttpServletResponse, HttpServletRequest, 然后呢就可以還原到我們以前的使用方式了.

直接訪問
雖然Struts2提供了ActionContext來訪問Servlet API,但是這種方式畢竟不能直接獲取Servelt API實(shí)例,為了在Action中直接訪問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í)例化,開發(fā)中我們會(huì)這樣做:
public abstract class BaseAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, SessionAware
然后讓我們每個(gè)模塊的Action來繼承這個(gè)BaseAction類,然后我們就可以在Action中直接使用Servelt的API了.

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多