首页 > 解决方案 > 具有多个属性和功能的自定义标签?

问题描述

这是在单个 tld 文件(即自定义标签)中写入多个属性和多个函数的正确方法。

这是代码中的标签:


     <tag>
        <name>cardwidgettags</name>
        <tag-class>com.sciformix.sciportal.apps.dap.CardWidget</tag-class>
        <body-content>empty</body-content>

        <attribute>
            <name>title</name>
            <required>true</required>
        </attribute>
        <attribute>
            <name>link</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>icon</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>




    </tag> 
    <function>
            <name>addLineItem</name>
            <function-class>.dap.CardWidget</function-class>
            <rtexprvalue>true</rtexprvalue>
            <function-signature>void addLineItem(java.lang.String, java.lang.String, java.lang.String)</function-signature>
    </function>
</taglib>

如何从 jsp 页面一次性调用它。

谢谢

标签: javajspjstl

解决方案


在您的 tld 文件中,您可以执行以下操作:

函数.tld

        <?xml version="1.0" encoding="UTF-8"?>
        <taglib version="2.1"
            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">

        <tlib-version>1.0</tlib-version>
        <short-name>functions</short-name>
        <uri>http://anyurl.com/myfunctions</uri>  <!-- define url to access functions from jsp file -->

        <function>
            <name>functionOne</name> <!-- function name -->
            <function-class>com.mypackage.MyUtilClass</function-class> <!-- full path to your class where define function -->
            <function-signature>void functionOne(java.lang.Integer, java.lang.Integer, java.lang.Integer)</function-signature>
        </function>

        <function>
            <name>functionTwo</name> <!-- function name -->
            <function-class>com.mypackage.MyUtilClass</function-class> <!-- full path to your class where define function -->
         <function-signature>void functionTwo(java.lang.String, java.lang.String, java.lang.String)</function-signature>
        </function>

  </taglib>

我的jsp.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="myfnc" uri="http://anyurl.com/myfunctions" %> <!-- url is defined above -->
...
${myfnc:functionTwo("str1","str2", "str3")}
${myfnc:functionOnw(1, 2, 3)}

推荐阅读