Java Web (vii) JSTL tag Library

Source: Internet
Author: User
Tags try catch

Before we learned on the JSP page in order not to use the script, so we have the JSP built-in behavior, behavior can only provide a small part of the function, most of the time will still use the Java script, and then use the El expression, basically El expression seems to meet our requirements, it can obtain a variety of objects, Various values, and do not throw errors such as nullpointerexception, but El expression functionality is still limited, such as cannot traverse the collection, so in order to perfect the JSP, so that it does not use Java code, there is a JSTL tag library, using Jstl and El with the use of , basically can realize all functions, display data, traverse data and so on.

    

First, why use the label?

JSP is used to display the data, in front of us in the JSP HTML embedded Java code, and <%%> and so on, mixed with the poor readability and maintainability, and the use of Java script is not easy to reuse code, to achieve more complex display functions, is not convenient, and the JSP tag and HTML code is very similar, using JSP tag appears JSP neat and clean, good readability, label can be reused, so as we said above, we slowly realize that the lack of natural in step by step improvement, from the use of JSP behavior, using El, in the use of tags, are for a purpose, Make JSP more complete, convenient, display data only, not embed logic code. This involves layering ideas, which are explained later.

  

Second, JSTL Tag library

Jstl Tag Library includes many kinds of tags core, FMT, FN method Library, SQL Tag library, using JSTL tag library, need to import jstl tag library with taglib directive

    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/> tags

Out: Outputs the specified content to the browser, which supports the El expression by default,

<c:out value= "" default= "" EscapeXML/>

Value: The values of the output, which can be an El expression

Default: The default property value is output if the value of the Value property does not exist

Excapexml: If True, the special character in value is escaped and is true by default

            

            

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

            

Only one usage of the set tag is described in the figure, where Var cannot accept El expressions, only characters,

<c:set target= "" property= "" value= ""/>

Target: Similar to Var, but it can accept El expressions, can be an object, a map, etc., and VAR to complement

Property: The name of the attribute in the object

Value: Property Assignment

            

What kind of set tag to use, depends on your needs.

      <c:if test= ""/> Label

If condition statement

            

      <c:choose/> Label

            

      <c:fortokens > tags

Splits the custom string by the specified character and traverses the output

Items: Custom string

Delims: Specifying characters

var: the variable name that is traversed.

            

      <c:catch > tags

Equivalent to a try Catch

            

            

var: the exception object after snapping

       <c:forEach/> tags

Enhanced for Loop,

Format:

<c:foreach items= "" var= ""/>

Items: objects that need to be traversed

var: used to store every item of traversal, stored in the page scope, and used only in the loop body

1. Items is a string or array of strings

For string, direct output, string array, traversal output

              

             

2. Items for list collection

              

3. Items for map Collection

              

4. Normal for loop

              

Begin: Starting from 1

End: Ends at 10

Step: Add 2 each time

var: the variable of the loop,

      <c:url/> Label

Remember when the session, if the cookie is disabled, then the URL can only be rewritten to allow the session to continue to take effect, and the use of the API is Response.encodeurl (); is to bring SessionID as a parameter, The function of URL tags is to implement similar functions.

<c:url value= "" var= "" scope= ""/> Explain three properties

Value: The URL to be overridden

var: If the variable is written, then its rewritten URL will be stored in the page scope, if the scope is not, the default is the page, otherwise the scope property value, the scope of the scope can be directly used to take this variable, as shown below.

Scope: Scope range, default is page,

              

Click on hyperlinks after access

The cookie is not turned off, so SessionID is not written to the URL.

              

      <c:redirect/> Label

          

    

      <c:import/> Label

Similar to the include behavior and include directives for JSPs, Jstl also provides label <c:import/> for include functionality, but the Import tab is more powerful and can even include Web pages on the Internet.

<c:import url= "http://www.baidu.com" charencoding= "Utf-8"/>

          

          

      

FMT Tag Library

is a number of ancillary functional tags, on the explanation of a put, not much use, and so on to learn in detail

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

Tag has only one property, value

<fmt:requestEncoding/> Set the encoding,

Equivalent to Request.setcharacterencoding ("") in Java;

Post submit Chinese parameters, use <fmt:requestencoding value= "Utf-8"/> To set the encoding, there will be no Chinese garbled.

Get submits the Chinese parameter, the label will be invalidated, unless in Tomcat's Server.xml, the urlencoding is set to UTF-8 (default is Iso-8859-1).

<fmt:setLocale/> Show data formats for all regions

Wait a minute

    fn Method Library

Provides methods, functions such as String lookup, interception, and so on. The FN tag library is not called the FN method library because it is not the same as the CORE,FMT label format and must be used in an El expression

Format: fn:methodname () format, for example, Fn:contains ()

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

Fn:contains (String string1,string string2);

Containsignorecase method that determines whether to include the specified string, ignoring the case

Fn:contains (String string1,string string2)

EndsWith method to determine whether a string ends with the specified argument,

Fn:endwith (String string1,string string2);

StartsWith method to determine whether a string starts with a specified argument

Fn:startswith (String string1,string string2);

EscapeXML method, whether the character is escaped

Fn:escapexml (source);

IndexOf method to find the position of the first occurrence of a string in another string

Fn:indexof (String string1,string string2);

The split method, which splits a string into multiple strings in a specified argument to form a string array

。。

Join method, which, in contrast to the split method, joins a string array as a parameter to a string

Length method, take string, array, set of lengths

Wait a minute

    SQL Tag Library

The JSP can operate the database directly, but most of the time we will not use it.

    XML Tag Library

For easy processing of XML,

          

Iii. Summary

Focus on the core of the Jstl in the tag library, in fact, some of the control process of the label, no thought, as long as it will be used on the line, after forgetting to look back to check, the most important thing is thought, these dead things recorded on the line.

Java Web (vii) JSTL tag Library

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.