Denis's Java Library

          The only documentation is the code itself

          自定義標(biāo)簽獲取struts中action的全部路徑(原創(chuàng))

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

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

          <html:form action="/checkId" >

          綜合了一下決定還是自己寫個taglib來實現(xiàn),其實只需要照著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-10 13:24 DenisLing 閱讀(1135) 評論(0)  編輯  收藏 所屬分類: struts


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 信丰县| 孟连| 城固县| 周口市| 嘉峪关市| 苗栗市| 延长县| 临猗县| 鱼台县| 屯昌县| 房产| 固安县| 鞍山市| 南皮县| 凤凰县| 石屏县| 长岭县| 太原市| 沁源县| 合川市| 东阳市| 巴彦县| 于田县| 绿春县| 静海县| 内江市| 兖州市| 昭通市| 嘉善县| 静安区| 凌海市| 内江市| 酒泉市| 进贤县| 皋兰县| 怀来县| 乳山市| 渭南市| 台北县| 虎林市| 策勒县|