jsp调用java方法 function taglib

1、新建tld文件:

my-functions.tld:

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">
    <description>functions</description>
    <tlib-version>3.0</tlib-version>
    <short-name>fn</short-name>
    <uri>http://mysteel.com/shiro/tags/mysteel-functions</uri>


    <function>
        <description>根据船code显示船名称</description>
        <name>shipName</name>
        <function-class>com.test.system.web.taglib.Functions</function-class>
        <function-signature>java.lang.String getShipName(java.lang.String)</function-signature>
    </function>


</taglib>

2、建立java类:

package com.test.system.web.taglib;


import com.portx.util.XmlReaderUtil;
import org.springframework.util.CollectionUtils;


import java.util.Collection;


public class Functions {

    public static String getShipName(String code) {

        return XmlReaderUtil.getValueByKey(code);
    }

}

3、在web.xml中定义:

<jsp-config>
        <taglib>
            <taglib-uri>/tags</taglib-uri>
            <taglib-location>/WEB-INF/tld/datetag.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/functions</taglib-uri>
            <taglib-location>/WEB-INF/tld/mysteel-functions.tld</taglib-location>
        </taglib>
    </jsp-config>

4、jsp中调用

<%@ taglib prefix="fnc" uri="/functions" %>


${fnc:shipName(item.shipCorpCd)}

即可调用。

原文地址:https://www.cnblogs.com/gmq-sh/p/5534743.html