Denis's Java Library

          The only documentation is the code itself

          自定義標簽獲取struts中action的全部路徑(原創)

                由于項目里面由需要一個form可以提交多個action(本來可以用 dispatch值來實現,后來考慮到要使用validator框架驗證)。后來考慮的方案為使用js來控制form的流向,例如
                
          form.action='/bookstore/checkId.do'

          不過新的問題來了!如何能不用hardcode而拿到我要的action的實際路徑呢?比如我定義的struts-config文件里面的action是 
          path="/checkId"
          但是實際解釋后的path是:
          action='/bookstore/checkId.do'
          前 綴和后面的后綴.do都是根據你的項目部署的路徑和你在web.xml中配置的mapping的后綴有關系,如果我把內容寫死到jsp中那以后我要是想把 checkId.do改成checkId.action那就要更改jsp,由于struts本來提供的幾個taglib里面的

          <html:form action="/checkId" >

          綜合了一下決定還是自己寫個taglib來實現,其實只需要照著struts 中的 FormTag.java 文件依葫蘆畫瓢就可以了,一下為本人的代碼部分

          StrutsActionCustomTag.java
          package com.denis.framework.common.taglib.strutsActionExt;

          import javax.servlet.http.HttpServletResponse;
          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.tagext.TagSupport;

          import org.apache.struts.taglib.TagUtils;

          public class StrutsActionCustomTag extends TagSupport {

             
          protected String actionName = null;
             
              
          public String getActionName() {
                 
          return actionName;
              }


              
          public void setActionName(String actionName) {
                 
          this.actionName = actionName;
              }


              
          public int doStartTag() throws JspException {

                  StringBuffer results 
          = new StringBuffer();
                  HttpServletResponse response 
          =
                      (HttpServletResponse) 
          this.pageContext.getResponse();
                 
                  results.append(response.encodeURL( TagUtils.getInstance().getActionMappingURL( 
          this.actionName , this.pageContext)) );

                  TagUtils.getInstance().write(pageContext, results.toString());

                 
          return (EVAL_BODY_INCLUDE);
              }


              
          public void release() {
                 
          super.release();
                 
          this.actionName = null ;

              }

          }


          tld定義部分

          framework-struts.tld
          <?xml version="1.0" encoding="UTF-8"?>

          <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
          <taglib>

          <tlibversion>1.0</tlibversion>
              <jspversion>1.1</jspversion>
              <shortname>framework</shortname>
              <uri>http://www.denisFramework.org/Framework-Tag</uri>
              <tag>
              <name>getActionUrl</name>
              <tagclass>com.denis.framework.common.taglib.strutsActionExt.StrutsActionCustomTag</tagclass>
              <bodycontent>empty</bodycontent>
              <attribute>
                   <name>actionName</name>
                   <required>true</required>
                   <rtexprvalue>true</rtexprvalue>
              </attribute>
          </tag>

          </taglib>



          ok ! 直接在jsp中如下使用即可取到action的真正路徑

           
          <%@ page language="java"%>
          <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
          <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
          <%@ taglib uri="/WEB-INF/framework-struts.tld" prefix="framework" %>
           
          <html> 
             
          <head>
                 
          <title>JSP for loginForm form</title>
             
          </head>
             
          <body>
                 
          <framework:getActionUrl actionName="login" />
                 
          <html:form action="/login">
                      name : 
          <html:text property="name"/><html:errors property="name"/><br/>
                      password : 
          <html:password property="password"/><html:errors property="password"/><br/>
                     
          <html:submit/><html:cancel/>
                 
          </html:form>
             
          </body>
          </html>



          大家要是有更好的解決方法希望能指正!謝謝!

          posted on 2005-11-21 15:52 DenisLing 閱讀(1860) 評論(1)  編輯  收藏

          評論

          # re: 自定義標簽獲取struts中action的全部路徑(原創) 2008-03-11 09:53 csch

          直接用lastIndexOf("/")和lastIndexOf(".")獲取最后一個"/"和"."的位置再截取這兩個位置之間的字符串好像也可以吧  回復  更多評論   


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


          網站導航:
           
          主站蜘蛛池模板: 阳高县| 从化市| 黑山县| 保德县| 会东县| 富川| 石渠县| 务川| 陇南市| 定日县| 宜宾市| 东阿县| 瓦房店市| 托里县| 石泉县| 临海市| 丘北县| 米林县| 仁寿县| 甘南县| 裕民县| 松江区| 宁南县| 临夏县| 呼伦贝尔市| 庆城县| 太仓市| 苍山县| 北海市| 英吉沙县| 神池县| 原平市| 凌海市| 邢台县| 彰化县| 田林县| 铜鼓县| 敦化市| 牡丹江市| SHOW| 大理市|