Denis's Java Library

          The only documentation is the code itself

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

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

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

          <html:form action="/checkId" >

          綜合了一下決定還是自己寫(xiě)個(gè)taglib來(lái)實(shí)現(xiàn),其實(shí)只需要照著struts 中的 FormTag.java 文件依葫蘆畫(huà)瓢就可以了,一下為本人的代碼部分

          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 閱讀(1139) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): struts


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 盐边县| 巴楚县| 三江| 肃宁县| 永顺县| 岑溪市| 福安市| 嵊州市| 武山县| 易门县| 富川| 新营市| 石门县| 怀宁县| 灌云县| 广德县| 怀集县| 杨浦区| 潢川县| 商都县| 陆河县| 津南区| 准格尔旗| 满城县| 密云县| 灵武市| 淅川县| 嘉定区| 普格县| 沛县| 文昌市| 保德县| 合川市| 承德县| 宿迁市| 上犹县| 且末县| 德阳市| 宁蒗| 望城县| 正宁县|