首页 > Web开发 > 详细

jsp-->self-defined tag with function tag in taglib

时间:2014-07-08 10:03:24      阅读:579      评论:0      收藏:0      [点我收藏+]

一.WEB-INFO下的*.tld自定义标签描述文件PAFTaglib.tld

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    <tlib-version>1.1</tlib-version>
    <short-name>paf</short-name>
    <uri>http://paf.taglib</uri>   

    /**<function>可以让我们在jsp中直接调用某个方法,根据自定义的方法返回指定的值,兼容jstl标签,省去了在jsp中直接使用<%!%>来定义方法体再调用的繁琐.如果你用过el语言的话估计会很快上手,其实<function>标签 就是一个拥有方法体的el语言.注意:function所定义的方法必须需要是静态的,如果不是静态的话jstl是不能识别所定义的方法;不用再继承TagSupport、BodyTagSupport、SimpleTagSupport中的一个*/

    <function>
        <name>getParamByServerBeanByPageNameByParamName</name>

        <!--specified handle class, full path-->

        <function-class>com.hp.it.techasmts.web.util.TaglibUtil</function-class>

        <!--specified actual handle method in handle calss,full path-->

        /**在<function-signature>需要写完整的类名,假如是String类型的话就必须写成java.lang.String,不支持泛型的定义如java.util.List<java.lang.String>,eclipse会把<>当作xml的 格式来判断的,所以就省略该泛型的定义.*/

        <function-signature>java.lang.String getParamByPageNameByParamName(
            com.hp.it.techasmts.web.data.RequestUtilServerBean,
            java.lang.String, java.lang.String)
        </function-signature>
    </function>
    <function>
        <name>getParameterByRequestPageNameByParameterName</name>
        <function-class>com.hp.it.techasmts.web.util.TaglibUtil</function-class>
        <function-signature>java.lang.String getParameterByRequestPageNameByParameterName(
            com.hp.it.techasmts.web.data.RequestPagesViewHelper,
            java.lang.String, java.lang.String)
        </function-signature>
    </function>
</taglib>


二.实际的处理类和方法TaglibUtil

public class TaglibUtil {
    /**
     * @param target
     *            the target bean
     * @param pageName
     *            the param used in the target bean
     * @param paramName
     *            the param used in the target bean
     * @return the target method result
     */

    public static String getParamByPageNameByParamName(RequestUtilServerBean target, String pageName, String paramName) {
        return target.getParamByPageNameByParamName(pageName, paramName);
    }

    public static String getParameterByRequestPageNameByParameterName(RequestPagesViewHelper target, String pageName, String paramName) {
        return target.getParameterByRequestPageNameByParameterName(pageName, paramName);
    }
}


三.在web.xml中引入定义的tld文件

<!-- enable call the customized method in the JSTL/EL expression in PAF -->
<jsp-config>
    <taglib>
        <taglib-uri>http://paf.taglib</taglib-uri>
        <taglib-location>/WEB-INF/PAFTaglib.tld</taglib-location>
    </taglib>

    /**<jsp-property-group>定义了一组JSP的特性,这些特性实际上对应JSP的 page directive定义的特性,但通过<jsp-property-group>可以方便地对多个具有相同属性的JSP统一定义。*/

    <jsp-property-group>
        <description>
            Special property group for JSP Configuration JSP example.
        </description>
        <display-name>JSPConfiguration</display-name>
        <url-pattern>*.jsp</url-pattern>
        <el-ignored>false</el-ignored>
        <page-encoding>utf-8</page-encoding>

        <!--若为true,表示不支持<%scripting%>语法-->

        <scripting-invalid>false</scripting-invalid>

        <!--设置JSP网页的抬头,扩展名为.jspf-->

        <!--include-prelude>xxx.jspf</include-prelude>

        <!--设置JSP网页的结尾,扩展名为.jspf-->

        <include-prelude>xxx.jspf</include-prelude-->

    </jsp-property-group>

</jsp-config>


四.在jsp中使用自定义标签

引入标签:<%@ taglib prefix="paf" uri="http://paf.taglib"%>

使用:

<c:choose>
    <c:when test="${paf:getParameterByRequestPageNameByParameterName(requestPagesViewHelper,‘SELECT_CUSTOMER‘, ‘DEFAULT_CUSTOMER_SELECTION‘)==‘filterCustomer‘}">
        $.slideTab({defaultIndex:1});// select by filter customer is default
    </c:when>           
    <c:when test="${paf:getParameterByRequestPageNameByParameterName(requestPagesViewHelper,‘SELECT_CUSTOMER‘, ‘DEFAULT_CUSTOMER_SELECTION‘)==‘selectByFile‘}">
        $.slideTab({defaultIndex:2});// select by upload file is default
    </c:when>                  
    <c:when test="${paf:getParameterByRequestPageNameByParameterName(requestPagesViewHelper,‘SELECT_CUSTOMER‘, ‘DEFAULT_CUSTOMER_SELECTION‘)==‘selectBySAIDs‘}">
        $.slideTab({defaultIndex:3});// select by SAID is default
    </c:when>                   
    <c:otherwise>
        $.slideTab({defaultIndex:2});// select by upload file while the param has not be set
    </c:otherwise>
</c:choose>


本文出自 “六度空间” 博客,请务必保留此出处http://jasonwalker.blog.51cto.com/7020143/1434986

jsp-->self-defined tag with function tag in taglib,布布扣,bubuko.com

jsp-->self-defined tag with function tag in taglib

原文:http://jasonwalker.blog.51cto.com/7020143/1434986

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!