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 阅读(1819) 评论(1)  编辑  收藏

评论

# re: 自定义标签获取struts中action的全部路径(原创) 2008-03-11 09:53 csch

直接用lastIndexOf("/")和lastIndexOf(".")获取最后一个"/"和"."的位置再截取这两个位置之间的字符串好像也可以吧  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: