Java Web (VII) JSTL tag library

Previously, we learned that in order not to use scripts on JSP pages, we have built-in behaviors and behaviors in JSP, which can only provide a small part of functions. Most of the time, we still use Java scripts, and then use El expressions. Basically, El expressions seem to meet our requirements. They can obtain various objects and values, And it will not throw errors such as NullPointerException, but the El expression function is still limited, such as not traversing collections. Therefore, in order to improve JSP and make it completely free of Java code, there is a JSTL tag library. Using the combination of JSTL and El, it can basically realize all functions, display data, traverse data, etc.

1、 Why use labels?

JSP is used to display data. Earlier, we embedded Java code in HTML in JSP and mixed it with <%% >, which is very poor in readability and maintainability. Moreover, using java script is not convenient for code reuse, and it is not convenient to realize complex display functions. JSP tags are very similar to HTML codes. Using JSP tags makes JSP clean and readable, Tags can be reused, so as we said above, we slowly realize the shortcomings and naturally improve step by step. From using JSP behavior, using el and using tags, we are all for one purpose to make JSP more complete and convenient. We only display data without embedding logic code. This involves the idea of layering, which will be explained later.

2、 JSTL tag library

JSTL tag library includes many kinds of tag core, FMT, FN method library and SQL tag library. To use JSTL tag library, you need to import JSTL tag library with taglib instruction

Core tag library

      <%@taglib uri=" http://java.sun.com/jsp/jstl/core " prefix="c" %>

Explain < C: out / >, < C: set / >, < C: remove / >, < C: if test = "" / >, < C: choose / >, < C: fortokens / >, < C: catch / >, < C: URL / >, < C: redirect / >, < C: foreach / >

< C: out / > label

Out: outputs the specified content to the browser. El expressions are supported by default,

      

Value: the output value, which can be an EL expression

Default: if the value of the value attribute does not exist, the default attribute value will be output

Excapexml: if true, special characters in value will be escaped. The default value is true

< C: set / > and < C: remove / > tags

In the figure, only one usage of the set tag is introduced. Var cannot accept El expressions, but only characters,

          

Target: is similar to VaR, but it can accept El expressions. It can be an object, a map, etc., which complement var

Property: the name of the property in the object

Value: attribute assignment

Which set tag to use depends on your own needs.

< C: if test = "" / > label

If conditional statement

< C: choose / > label

< C: fortokens > tags

The custom string is divided according to the specified characters, and the output is traversed

Items: custom string

Delims: Specifies the character

Var: the name of the traversal variable.

< C: catch > label

Equivalent to try catch

Var: exception object after capture

< C: foreach / > label

Enhanced for loop,

Format:

             

Items: objects to traverse

Var: used to store each traversal item. It is stored in the page scope and can only be used in the loop body

1. Items is a string or string array

For string, direct output, for string array, traversal output

2. Items is a list set

3. Items is a map set

4. Normal for loop

Begin: start from 1

End: end at 10

Step: add 2 each time

Var: loop variable,

< C: URL / > tag

Remember when talking about session, if the cookie is disabled, you can only rewrite the URL to make the session continue to take effect, and the API used is response encodeURL(); Is to bring sessionid as a parameter, and the URL tag is used to achieve similar functions

< C: URL value = "" var = "" scope = "" / > just explain the three attributes

Value: URL to rewrite

Var: if this variable is written, the rewritten URL will be stored in the page scope. If there is no scope, it is page by default. Otherwise, the scope attribute value shall prevail. You can directly use this variable within the scope, as shown below.

Scope: scope. The default is page,

Click the hyperlink after visiting

The cookie is not closed, so the session ID will not be written to the URL.

< C: redirect / > label

< C: import / > label

Similar to the include behavior and include instruction of JSP, JSTL also provides the tag of include function < C: import / >. However, the function of import tag is more powerful and can even include web pages on the Internet.

          

FMT tag library

It's some auxiliary function labels. Just explain one. I don't use much. I'll learn it in detail when I use it

        <%@ taglib uri=" http://java.sun.com/jsp/jstl/fmt " prefix="fmt"%>

The tag has only one attribute, value

< FMT: requestencoding / > set the encoding,

It is equivalent to request. In Java setCharacterEncoding("");

Post submit Chinese parameters and use < FMT: requestencoding value = "UTF-8" / > to set the encoding, so that Chinese random codes will not appear.

Get submits Chinese parameters, and the tag will become invalid unless you go to Tomcat's server XML, set urlencoding to UTF-8 (the default is iso-8859-1).

< FMT: setlocale / > display the data format of all regions

Wait

FN method library

Provide some methods and functions, such as string lookup and interception. It is not called FN tag library but FN method library because it is different from core and FMT tag format and must be used in El expression

Format: FN: methodname(), for example, FN: contains()

The contains method determines whether the specified string is included, which is equivalent to the contains method of the string class in Java.

           fn:contains(String string1,String string2);

Containsignorecase method to judge whether the specified string is included, ignoring case

           fn:contains(String string1,String string2)

The endswith method determines whether a string ends with a specified parameter,

           fn:endWith(String string1,String string2);

Startswith method to judge whether a string starts with the specified parameter

           fn:startsWith(String string1,String string2);

Escapexml method, whether to escape characters

           fn:escapeXml(source);

Indexof method to find the first occurrence of a string in another string

           fn:indexOf(String string1,String string2);

Split method, which divides the string into multiple strings bounded by the specified parameters to form a string array

           。。

The join method, in contrast to the split method, connects a string array into a string bounded by parameters

The length method takes the length of string, array and collection

Wait

SQL tag library

The database can be operated directly in JSP, but we won't use it most of the time..

XML tag library

To facilitate XML processing,

3、 Summary

I focused on the core tag library in JSTL, which is actually some tags that control the process. There is no idea. Just use it. If you forget it later, go back and check it. The most important thing is the idea. Just record these dead things.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>