开发者

Alternative for fn:startsWith(var,'value') - jsp

I have the this piece of code on my ap开发者_如何学JAVAplication:

<c:when test="${fn:startsWith(var,'value')}">
    <c:set var="other_var" value="x"></c:set>
</c:when>

But I just find out that I am not allowed to use any version above 1.0 of standard taglib.

Do I have any good alternative to use here?


Create an EL function yourself.

package com.example;

public final class Functions {
     private Functions() {}

     public static boolean startsWith(String string, String pattern) {
         return string.startsWith(pattern);
     }
}

Create /WEB-INF/functions.tld which look like follows:

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
    <display-name>Custom Functions</display-name>    
    <tlib-version>1.0</tlib-version>
    <uri>http://example.com/functions</uri>

    <function>
        <name>startsWith</name>
        <function-class>com.example.Functions</function-class>
        <function-signature>boolean startsWith(java.lang.String, java.lang.String)</function-signature>
    </function>
</taglib>

Use it as follows:

<%@taglib uri="http://example.com/functions" prefix="f" %>

<c:if test="${f:startsWith(var, 'value')}">
    ...
</c:if>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜