In Java, we can directly write string STR = str1 + str2 in this way. In JSP, if the El expression is used, ${1 + 2} returns 3, $ {'xxx' + 'xxx'} is incorrect, because El calculates it and throws an exception in Java. lang. numberformatexception: for input string: "XXX", but many times we may have this requirement. The following describes how to write a custom El expression.
First, create a Java class elfuncutil, which contains static
The function is to return the string after the two parameters are added.
/*** Class description: ** @ author yjde */public class elfuncutil {/*** El method is used to connect two strings ** @ Param str1 * @ Param str2 * @ return */public static string append (string str1, string str2) {return str1 + str2 ;}}
Then write a TLD file elfunc. 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 [url] Protocol" version = "2.0"> <tlib-version> 1.0 </tlib-version> <short-Name> elf </short-Name> <function> <description> used to connect two strings </description> <Name> append </Name> <function-class> COM. ourpalm. MIS. common. util. elfuncutil </function-class> <function-signature> JAVA. lang. string append (Java. lang. string, Java. lang. string) </function-signature> <example >$ {elf: append (str1, str2) }</example> </function> </taglib>
Finally, we can directly use our custom El expression in JSP. Do not forget to import TLD in JSP before use.
<%@ taglib prefix="elf" uri="/WEB-INF/tlds/elfunc.tld"%>
<c:out value="${elf:append(param.str1, param.str2)}"/>