解决自定义EL函数Eclipse报the function is undefined错误

写一个小项目时遇到须要将一个长字符串转成小段加点点点的形式显示,立刻想到了定义一个自定义函数java

package cn.harmel.common.util;

/**
 * 操做字符串的工具类
 * 
 * @author Harmel
 *
 */
public final class StringUtils {

    private StringUtils() {

    }
    
    public static String transform(String str, int length) {
        if (str.length() > length) {
            return str.substring(0, length) + "...";
        }
        return str;
    }

}

按照惯例,须要在/WEB-INF下的任意目录(tags除外)定义一个tld文件,我定义在/WEB-INF/tag里web

<?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>harmel's tag</description>
    <tlib-version>1.0</tlib-version>
    <short-name>h</short-name>
    <uri>http://www.harmel.cn/tag</uri>
    
    <function>
        <name>subStr</name>
        <function-class>cn.harmel.common.util.StringUtils</function-class>
        <function-signature>java.lang.String transform(java.lang.String, int)</function-signature>
    </function>
    
</taglib>

大功告成,立刻在JSP页面中导入标签使用jsp

<%@ taglib prefix="h" uri="http://www.harmel.cn/tag" %>
${h:subStr("123456789012345", 10)}

测试经过接着把该函数用到了其余页面中,修改了文件Eclipse立马build workspace后发如今/WEB-INF下的JSP页面上面都有个大红叉叉,而其/WEB-INF外的JSP页面却没有。提示说the function h:subStr is undefined可是部署运行却没有问题。如今的Tomcat容器已经能够自动搜索/WEB-INF下的tld了,想一想多是Eclipse不够智能为了解决强迫症只好在web.xml中给出了标签订义:函数

<jsp-config>
    <taglib>
        <taglib-uri>http://www.harmel.cn/tag</taglib-uri>
        <taglib-location>/WEB-INF/tag/harmel.tld</taglib-location>
    </taglib>
</jsp-config>

成功将强迫症解决掉
工具

相关文章
相关标签/搜索