JSP Custom Label Series--- usage strength __js

Source: Internet
Author: User
Tags tld

Before the example has been written, because the time relationship has not been released, this time to bring about the Taglib <tag> in the basic functions have been introduced, in taglib we found that there is a tag called <function> this brief introduction < Basic usage of function> tags what can,<function> tags do? It allows us to call a method directly in the JSP, return the specified value according to the custom method, and the compatible JSTL tag, eliminating the direct use of the <%!% in the JSP > To define the complexity of the method body invocation. If you use the El language, you'll be able to get started quickly, but the <function> tag is an El language with a method body. Note: The method defined by the function must be static, JSTL does not recognize the defined method if it is not static.

The Java code is as follows:

Package org.lxh.taglib;

Import java.util.List;

public class Functiontag {public

	static string Hello (string name) {return

		name;
	}

	public static Integer BBQ (List list) {return

		list.size ();
	}
}

The method must be static, and you can define a method that has a return value or a void type.

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 "> <tlib-version>1.0</tlib-version> <short-name >my</short-name> <uri>http://lauedward.spaces.live.com</uri> <function> <!-- El page invoke name--> <name>hello</name> <!--Specify the processing class for the label, specifying which Java class The label is to handle. --> <function-class>org.lxh.taglib.FunctionTag</function-class> <!-- Specifies the method that is actually called in the name of the El page call. Specifies the actual method to process the class. Parameters and callback functions to write the full path--> <function-signature>java.lang.string Hello ( java.lang.String) </function-signature> </function> <function> <name>bbq</name> <f Unction-class>org.lxh.taglib.functiontag</function-class> <function-signature>java.lang.integer BBQ (java.util.List) </function-signaTure> </function> </taglib> 

Note: In <function-signature> need to write the complete class name, if it is a string type must write java.lang.String such words, does not support the definition of generics such as java.util.list< Java.lang.string>eclipse will judge <> as an XML format, so omit the definition of the generic.

Jsp:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<% @taglib prefix= "My" uri= "/web-inf/tld/testfunction.tld"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<% @page import= "java.util.*"%><meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>

<%
list<string> list = new arraylist<string> ();
List.add ("AA");
List.add ("BB");
List.add ("CC");
Request.setattribute ("list", "Helloword");
Request.setattribute ("name", list);
Map map = new HashMap ();
Map.put ("1", "a");
Map.put ("2", "B");
Map.put ("3", "C");
Map.put ("4", "D");
%>

<br>
${my:hello (List)}
<br>
${MY:BBQ (name)}
<br>

</body>

Related Article

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.