JSTL Core Tag Library use

Source: Internet
Author: User
Tags control label

There are 13 tags in the JSTL core tag library, functionally divided into 4 categories:

1. Expression control Tags: out,set,remove,catch

2. Process Control Label:if,choose,when,otherwise

3. Loop Label:ForEach,fortokens

4.URL action tag:import,URL,redirect

When using tags, be sure to include the following code in the JSP file header:

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


These labels are described separately below:

1. <c:out> to display the contents or results of a data object (string, expression)

Java scripts are used in the following ways: <% out.println ("Hello")%> <% = Expression%>

Use the JSTL tag: <c:out value= "string", for example:

The result of the expression used is NULL, the default value is output

Then the page display effect is:


2. <c:set> is used to access variables in the JSP scope or in the JavaBean property. The following example assumes that the Person.java file is already in the class.

Press CTRL + C to copy the code

Press CTRL + C to copy the code

A total of four syntax formats, the first two are to give the scope of the JSP variable assignment, the latter two is to assign a value to the JavaBean variable

The effect is as follows:

3.<c:remove> is used primarily to remove specified variables from the specified JSP scope. Using a similar, only syntax is given below:

<c:remove var= "variable name" [scope= "Page|request|session|application"]></c:remove>


4.<c:catch> is used to handle exceptions generated in JSP pages and to store exception information

<c:catch var= "Name1" >

Code that easily generates exceptions

</c:catch>

If an exception is thrown, the exception information is stored in the variable name1.

5.<c:if>

<c:if test= "Condition 1" var= "name" [scope= "Page|request|session|application"]></c:remove>

Cases:

Press CTRL + C to copy the code

Press CTRL + C to copy the code

Effect:

6. <c:choose> <c:when> <c:otherwise> Three tags are usually nested, the first label is in the outermost layer, and the last label can only be used once in a nested set.

Cases:

Press CTRL + C to copy the code

Press CTRL + C to copy the code

7.<c:foreach>

Syntax: <c:foreach var= "name" items= "Collection" varstatus= "Statusname" begin= "Begin" end= "End" step= "Step" ></c: Foreach>

The label iterates through the elements in the collection Collection based on the loop condition. var is used to store the elements taken out of the collection; items specifies the collection to traverse; Varstatus is used to hold the information for the elements in the collection. Varstatus has 4 state attributes, as explained in the following example:

JSTL:--four properties of the foreach Tag instance: location, i.e. index: Total number of iterations: Whether it is the first position: is the last position:

Display effect:

8.<c:fortokens> is used to browse strings and to intercept strings based on a specified string
Syntax: <c:fortokens items= "Stringoftokens" delims= "delimiters" [var= "name" begin= "Begin" end= "End" step= "Len" varstatus= "Statusname"]></c:fortokens>

Let's look at an example:

                 jstl:  -- fortokens Tag Examples                                                                                                                                                             Four Properties:          location, that is, index:                          Total number of iterations:                         is the first position:                         is the last location:

Show Results:

9.URL Operation label

(1) <c:import> include other static or dynamic files on the JSP page. The difference from <jsp:include> is that the latter can only contain files from the same web app, which could include files from other Web applications, even resources on the network.

Syntax: <c:import url= "url" [context= "context"] [value= "value"] [scope=] ... "[charencoding=" Encoding "]></C: Import>

<c:import url= "url" varreader= "name" [context= "Context"][charencoding= "encoding"]></c:import>

Look at an example:

JSTL:--Import Tag instance

Show Results:

The URL path has an absolute path and a relative path. Relative path: <c:import url= "A.txt"/> Then, a.txt must be placed in the same file directory as the current file. If you start with "/", it means that the root directory of the application is stored in the root directory, such as the WebApps of the Tomcat application. Import B.txt under this folder: <c:import url= "/b.txt" >. If you want to access other web apps in the WebApps Management folder, use the context property. For example, to access the index.jsp under Demoproj, then: <c:import url= "/index.jsp" context= "/demoproj"/>.

(2) <c:redirect> This tag is used to implement the redirect of the request. For example, the user name and password entered by the user are validated, and the unsuccessful redirect to the login page. or implement the interface between different modules of the Web application

Syntax: <c:redirect url= "url" [context= "Context"]/>

Or: <c:redirect url= "url" [context= "Context"]>

<c:param name= "name1" value= "value1" >

</c:redirect>

Look at an example:

Lihui 11111

After running, the page jumps to: http://127.0.0.1:8080/?uname=Lihui&password=11111

(3) <c:url> used to dynamically generate a String-type URL that can be used in conjunction with the previous tag, or you can use the HTML <a> tag experiment hyperlink.

Syntax: <c:url value= "Value" [var= "name"] [scope= "..."] [context= "Context"]>

<c:param name= "name1" value= "value1" >

</c:url>

Or: <c:url value= "Value" [var= "name"] [scope= "..."] [context= "Context"]/>

Look at an example:

Use the URL tag to generate a dynamic URL and store the value in session. Tomcat Home

Show:



JSTL Core Tag Library use

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.