Introduction to the Foundation of tld Custom Label

Keywords: JSP Java xml encoding

Notes on tld

tld is the abbreviation of taglib description. Its custom tags are usually used in jsp pages, and tld is usually used in web projects.

jstl and c tags are used for validity judgment and permission judgment, which restrict and restrict some front-end page tags.

Many people only know this type of tag, but they don't know what it does and how to use it, so learn to use tld custom tag pairs

The project will be very helpful. Next I will use a self-written super simple demo (demo will give a link at the end of the article, programmers who need it can do it)

To illustrate how to use tld, customize tags, and make validity judgments.


2. Case description

1. Infrastructure diagram:



  2. tld custom tagging method requires the following steps:

(1) Create a new tld file under WEB-INF and write the following code into the tld file:

<?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>isnull</short-name><!-- Define short names for tags -->
	 
	 <!-- The parameters of the custom tag are all the parameters passed in the domain; the custom tag is basically used in the domain. jsp On the page -->
	<function>
	   <description>Judging whether the delivered content is empty</description><!-- Description of the label -->
	   <name>hasvalue</name><!-- Define the label name, after the short label -->
	   <function-class>util.Tld_util</function-class><!-- Class Paths for Tag Processing Domain Values  -->
	   <function-signature>boolean isnull(java.lang.String)</function-signature><!-- Specific Class Method for Tag Processing Domain Values -->
	   <example>${isnull:hasvalue(obj1)}</example><!-- Demonstration of the use of custom tags. Domain parameters are automatically passed to specific methods-->
	</function>

</taglib>
Every parameter I have defined is illustrated, basically self-modifying, and the class path and method of processing domain values with custom tags

Need to be consistent with the path and method of the corresponding project. Here we use the class path and method of Ben demo.


(2) Under the Testtld.java class, get the value submitted by the index.jsp page and put it into the domain:

 index.jsp:

<form action="testtld" method="post">
 <input type="text" name="testtld" value="123">
 <input type="submit" value="Submission">
</form>
 

 Testtld.java:

String getvalue=request.getParameter("testtld");
request.getSession().setAttribute("testtld", getvalue);//Put it in session domain
response.sendRedirect(request.getContextPath()+"/test.jsp");
       

(3) Refer to a custom tag on the test.jsp page, where you determine whether the value of tld is 3, and use different html tags:

<c:choose>
	<c:when test="${isnull:hasvalue(testtld)}">
	<p>tld The value is 123.</p>
	</c:when>
	<c:otherwise>
	<p>tld The value is not 123</p>
	</c:otherwise>
</c:choose>
<p>Get the domain value: ${testtld}</p>


(4) Define the classes and methods for processing domain parameters. Tld_util.java is used for judging domain values, since such methods have been specified in the tld file.

So this method is automatically executed to judge the domain value when the jsp page is referenced:

public class Tld_util {
	
	public static boolean isnull(String testtld){
		String value = "123";
		System.out.println("Gets the values in the domain:"+testtld);
		if(testtld!=null && testtld.equals(value)){
			return true;
		}
		return false;		
	}
}
    
3. Concluding remarks

1. tld custom tags can be effectively used in many projects to determine the global authority or validity of the front-end, which greatly speeds up the process.

I believe that many programmers feel the same about the progress of the project, because only a slight modification of the method of processing domain values can control html well.

Or the use of js tags;

2. The use of TLD is not very difficult. If the blog is not very clear, you can download the demo provided by this blog by yourself.

Very simple architecture, try it on your own several times, and look at the annotations provided in demo, then you can get started quickly, and then you can go on your own.

Test custom tags;

    3. Attach a link to demo and download it if necessary: http://download.csdn.net/detail/alan_liuyue/9832456;


Posted by Maiku on Wed, 03 Jul 2019 17:45:15 -0700