Studying Java......

          統計

          留言簿(1)

          積分與排名

          JavaServerFaces

          Mail Link

          Open Source

          友情鏈接

          收藏的鏈接

          閱讀排行榜

          評論排行榜

          利用函數computeURL( )實現同一FORM的多動作提交

                 在實際處理的頁面中,往往在一個頁面中有多個觸發的動作,而Struts的ActionForm中只能指定一個Action,是一種粗粒度的實現(JSF中有更好的解決方案),computeURL( )可以提供一種變通的解決方法.
                 computeURL( )是在org.apache.struts.util.RequestUtils(Struts Ver1.1)與org.apache.struts.taglib.TagUtils(Struts Ver1.2)類中的一個函數,用來解析基于Forward,Action,鏈接,頁面參數的URL可以用來動態改變頁面中Form對應的Action.Ver1.1中有以下兩種:
          1.computeURL(javax.servlet.jsp.PageContext pageContext, java.lang.String forward, java.lang.String href, java.lang.String page, java.util.Map params, java.lang.String anchor, boolean redirect)
          2.computeURL(javax.servlet.jsp.PageContext pageContext, java.lang.String forward, java.lang.String href, java.lang.String page, java.lang.String action, java.util.Map params, java.lang.String anchor, boolean redirect)
          其中第一個是  Deprecated.第二個在新版本中得以保留,另外還提供了另外一種重載:
          computeURL(javax.servlet.jsp.PageContext pageContext, java.lang.String forward, java.lang.String href, java.lang.String page, java.lang.String action, java.lang.String module, java.util.Map params, java.lang.String anchor, boolean redirect, boolean encodeSeparator) 
          參數說明如下:
          Parameters:
          pageContext - PageContext for the tag making this call
          forward - Logical forward name for which to look up the context-relative URI (if specified)
          href - URL to be utilized unmodified (if specified)
          page - Module-relative page for which a URL should be created (if specified)
          action - Logical action name for which to look up the context-relative URI (if specified)
          params - Map of parameters to be dynamically included (if any)
          anchor - Anchor to be dynamically included (if any)
          redirect - Is this URL for a response.sendRedirect(

          下面介紹一下詳細的使用方法:
          1.在JSP頁面中導入對應的包:
          <%@ page import= "org.apache.struts.util.RequestUtils"%>

          <%@ page import= "org.apache.struts.taglib.TagUtils"%>
          2.創建一個JAVASCRIPT函數:
          <script language="JavaScript" type="text/javascript">
          function search() {
           <%String searchUrl = RequestUtils.computeURL(
             pageContext,
             null,
             null,
             "/Search.do",
             null,
             null,
             null,
             false);
           %>
             
           document.form1.action = "<%=searchUrl%>";
           document.form1.submit();  
          }
          </script>
          3.在JSP頁面中給對應的表單指定ID以便上面的函數進行確定提交的是哪個FORM(如果一個頁面在存在多個FORM的話):
          <html:form styleId="form1" action="/aotherSearch">
          .........
          </html:form>
          4.在需要觸發提交動作的地方,調用2中的JAVASCRIPT函數:
          <html:button property="searchInfo" value="檢索" onclick="search()" style="width:100px" />

          對應的ACTION與FORM在配置文件中定義.這樣,就可以動態更改FORM的ACTION實現一個FORM對應多個ACTION了.

          posted on 2005-11-24 11:29 Terence 閱讀(1626) 評論(2)  編輯  收藏 所屬分類: STRUTS

          評論

          # re: 利用函數computeURL( )實現同一FORM的多動作提交 2005-11-27 13:22 DenisLing

          果然是個好方法!我以前的做法仿照了HtmlTag.java里面的做法提取了真正的action路徑,然后自己寫了個taglib來實現的!麻煩多了  回復  更多評論   

          # re: 利用函數computeURL( )實現同一FORM的多動作提交 2005-11-29 11:02 IBM

          用js不是也可以隨時改變嗎?
          根據傳入js的參數來決定form的action的地址是哪一個~  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 云浮市| 普安县| 渭源县| 扶余县| 乐业县| 宁晋县| 仪陇县| 丹凤县| 屯昌县| 简阳市| 广东省| 佛冈县| 烟台市| 如东县| 鄯善县| 北安市| 开阳县| 莱阳市| 平利县| 邹城市| 二手房| 临安市| 冕宁县| 沿河| 商水县| 西乌| 龙江县| 扶绥县| 图木舒克市| 南乐县| 天水市| 阿尔山市| 平顶山市| 腾冲县| 化隆| 罗甸县| 和静县| 德江县| 大渡口区| 来凤县| 神池县|