A detailed explanation of the custom El expression method

Source: Internet
Author: User

In Java we can connect strings directly to write String str=str1+str2, in the JSP in the El expression ${1+2} result is 3, and ${' xxx ' + ' xxx '} is wrong, because El has done it, Throws an exception Java.lang.NumberFormatException:For input string: "XXX", but many times we may have such a requirement, so the following is a brief introduction to the custom El expression.

First, you build a Java class Elfuncutil, which has a static method that returns a string of two parameters added.

/**
 * Class Description:
 * 
 * @author yjde/public
class Elfuncutil {
	/**
	 * El method to connect two strings * * 
	 @param str1
	 * @param str2
	 * @return
	/public static string append (String str1, String St R2) {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"
Http://www.w3.org/2001/XMLSchema-instance "
	xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee [url]http:// Java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd[/url] "
	version=" 2.0 ">
	<tlib-version>1.0 </tlib-version>
	<short-name>elf</short-name>
	<function>
		<description > for connecting 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>

Finally, we can use our custom El expressions directly in our JSP, and don't forget to import the TLD in the JSP before using it.

<%@ taglib prefix= "elf" uri= "/web-inf/tlds/elfunc.tld"%>
<c:out value= "${elf:append (PARAM.STR1, PARAM.STR2)}"/>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.