10.javaweb Core Tag Library detailed

Source: Internet
Author: User
Tags control label

I. JSTL introduction and installation configuration in the project

1, Introduction

The purpose of using the JSTL tag is not to expect Java logic code to appear in the JSP


2, installation configuration of Jstl

The individual tag library profiles in the jar package are first copied to the project Web-inf directory for use

Then copy the entire jar package to the Tomcat/lib directory

3, Jstl simple to use

For ease of use, you can also configure the mapping path of the tag library in the Web. xml file

After that, you can load the tag library by mapping the path name

Second, the core tag library

1, requirements: Use the core tag library to provide the main tags to complete the judgment, iteration, output and other common operations

2, main label

3. Expression-Control statements

3.1<c:out> tags use : instead of <% out.println ("strings")%> or <%= expressions in Java scripts%>

Function: The content or result used to output a data object (a string or an expression). The use of Jstl and El expressions are inseparable, El Although the results can be returned directly to the page, but sometimes the empty,<c:out> for this has a specific function processing (default)


Escapxml indicates whether some special characters are HTML-encoded and output, by default True

Usage examples

use of 3.2<c:set> tags

Function: Used to place an object within a specified domain range, or to store an object in a specified map or JavaBean object


Grammar1:stored value, placing a value within the specified domain range.
     <c:set value= "value1 "var=" name1 "[scope=" Page|request|session|application "]/>
Meaning: A variable named name1 with a value of "value 1" is stored within the specified scope.
     <c:setvar= "Name2" [scope= "Page|request|session|application"]>
Meaning: A variable named name2 with a value of 2 is stored within the specified scope scope.
    <c:set value= "value3 "target=" JavaBeanObject"property="Property name"/>
Meaning: Assigns a value of "value 3" to the property name of the specified JavaBean. Quite with the setter () method.
    <c:settarget= "JavaBeanObject"property="Property name">
Meaning: Assigns a value of 4 to the property name of the specified JavaBean.
The effect of Syntax 1 and Syntax 2, Syntax 3 and syntax 4 is the same, except that the value is placed in a different position, as for the use of that according to personal love, Syntax 1 and Syntax 2 is to store a value in scope range, Syntax 3 and syntax 4 are assigned to the specified JavaBean.

Tag properties:

Usage examples

The object exists in the specified domain range

Setting the JavaBean property value

To save the value of a data object in the Map collection

Equivalent to inserting a pair of key values, and then the corresponding value can be found by the key

Use of 3.3<c:remove> tags

Function: Removes the specified property from the specified JSP scope

Syntax: <c:remove var= "variable name" [scope= "Page|request|session|application"]> where scope can be omitted

Usage examples

Use of 3.4<c:catch> tags

Function: Used to capture the exception thrown by the content nested within the label body

Syntax: <c:catch [var= "VarName"]> may produce an exception for code </c:catch>. The Var attribute is used to identify the caught exception, which is saved in the page field

Usage examples

Use of 3.5<c:if> tags

Function: Same as Program if statement


Syntax one: no label body

<c:iftest= "testcondition" var= "VarName" [scope= "{page|request|session|application}"]/>

Syntax two: Tagged body

<c:if test= "Testcondition" [var= "VarName"][scope= "{page|request|session|application}"]>
Tag Body content

parameter description: (1)thetest attribute is used to store the judging condition, usually EL An expression

(2) The var attribute specifies the name of the property that holds the result of the decision,true or false< /c8>

(3) The scope attribute is used to specify the range in which the var attribute is stored

Use example: Design a landing page tagtest.jsp, save the login in the session field, And respond according to the landing situation

Enter admin, click Login

3.6 Process Control Label: <c:choose>,<c:when>,<c:otherwise> Complete Switch


<c:choose> as parent label

<c:whentest= " Condition One ">// Business Logic </c:when>

<c:whentest= " condition Two ">// Business Logic </c:when>

<c:otherwise>// Business Logic </c:otherwise>

usage Examples :

3.7 Loop Label:<c:foreach>

Function: Iterates through the elements in the collection according to the loop condition, and if the collection is not set, the same as for () looping and executes the label body until end and the loop value is saved in Var


Var= "Name"
Items= "Collection"
Varstatus= "Statusname"
Begin= "Begin"
End= "End"
step= "Step" >
Ontology content

Parameter Resolution ":
(1) var sets the variable name to store the elements taken out of the collection.
(2) items specifies the collection to traverse.
(3) Varstatus sets the variable name, which is used to hold the information (index value, number of cycles, etc.) of the elements in the collection.
(4) Begin, end is used to specify the starting and ending positions of the traversal (optional).
(5) Step specifies the step of the loop (that is, the number of elements passing through each cycle).

Varstatus has four status attributes

Usage examples


Do not traverse the collection, only as a loop function, the following JSP print 10 rows and 10 columns of the table, the table content is the product of the row. If used with <c:if>, the function of while () loop can be realized

3.8 Loop Labels <c:forTokens>

Function: Splits a string into a substring array according to the specified delimiter and then iterates over them, and if no delimiter is specified, the entire element is intercepted and used in the actual application to remove certain characters from the page


<c:fortokensitems= "Strigoftokens"
Delims= "Delimiters"
[var= "Name"
Begin= "Begin"
End= "End"
step= "Len"
varstatus= "Statusname" >
Ontology content

"Parameter description"
(1) Items specify the string to be iterated.
(2) delims specifies the delimiter used.
(3) var specifies the member to be used to store the traversal.
(4) Begin specifies the starting position of the traversal (int type starts with the value 0).
(5) End specifies the position of the end of the traversal (int, the last element in the default collection).
(6) Step traversal step (integral type greater than 0).
(7) Varstatus The state information of the member that is traversed.

Usage examples

Use of 3.9<c:import> tags

Function: This tag can contain other static or dynamic files to this JSP page, the difference with <jsp:include> is that:<jsp:include> can only contain files in the same Web application. <c:import> can include files from other web apps, even resources on the network.


"Syntax 1":
Url= "url"
[context= "Context"]
[value= "Value"]
[scope= "Page|request|session|application"]
[charencoding= "Encoding"]/>

"Syntax 2":
Url= "url"
Varreader= "Name"
[context= "Context"]
[charencoding= "Encoding"]/>

"Parameter description":
(1) The URL is the path to the resource, and the system throws an exception when the referenced resource does not exist, so the statement should be captured in the <c:catch></c:catch> statement block.
(2) There are two ways of referencing resources: absolute and relative.
An example of using an absolute path is as follows: <c:importurl= "http://www.baidu.com" >
Examples of using relative paths are: <c:importurl= "Aa.txt" >,aa.txt in the same file directory.
(3) If you start with "/" to indicate the root directory of the app. For example, the root folder of the Tomcat application is WebApps. The file Bb.txt under Import WebApps is written as: <c:import url= "/bb.txt" >
If you access other Web apps in the WebApps Management folder, you will use the context property.
(4) The context property is used to specify the root directory when accessing files from other Web applications. For example, the implementation code for the index.jsp under the access root is: <c:import url= "/index.jsp" context= "/root" >
Equivalent to webapps/root/index.jsp
(5) Var, scope, charencoding, varreader are optional attributes.

Usage examples

Importing Web pages with absolute paths

Referencing files in this app using relative paths

Store the imported files in a domain, and then pass the <c:out> output

3.10<c:url> Label

function tags are used to construct a URL address in a JSP page whose main purpose is to implement URL rewriting.


Syntax 1: Specifies that a URL is not modified, and you can choose to store the URL in a different range of JSPs.

Value= "Value"
[var= "Name"]
[scope= "Page|request|session|application"]
[context= "Context"]/>

Syntax 2: A mate <c:param> tag adds a specified parameter and parameter value to the URL, optionally storing the URL in name.
Value= "Value"
[var= "Name"]
[scope= "Page|request|session|application"]
[context= "Context"]>
<c:paramname= "parameter name" value= "Value" >

Use instances (in a browser-visible annotation method, no special characters appear, or an error may occur)

Click on "Baidu homepage (without parameters)"

Click on "Baidu homepage (with parameters)"

3.11<c:redirect> Label

Function: This tag is used to implement the redirect of the request. You can also add the specified parameters to the URL with the <c:param> tag.


"Syntax 1":
<c:redirect url= "url" [context= "Context"]/>
"Syntax 2":
<c:redirect url= "url" [context= "Context"]>
<c:param name= "name1" value= "value1" >
"Parameter description":
(1) The URL specifies the address of the redirected page, which can be either an absolute or relative address of type string.
(2) context is used to import pages in other web apps. When you want to use relative paths to redirect to other Web resources under the same server, the context property specifies the name of the other Web application

Usage examples

When the server resolves this jsp, it redirects the address to the Baidu home page and passes the parameters

3.12<c:param> Label

When you make a URL-related operation on a JSP page, you often append some parameters to the URL address. <c:param> tags can be nested within <c:import>, <c:url>, or <c:redirect> tags, attaching parameters to the URL addresses used by these tags.
<c:param> tags when attaching parameters to a URL address, the parameter values are automatically URL-encoded, for example, if the parameter value passed is "China", then it is converted to "%D6%D0%B9%FA" and then appended to the URL address, which is the use of <c: Top Benefits of Param> tags

10.javaweb Core Tag Library detailed

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.