Analysis of jsp-c tag usage instance and jsp tag instance

Source: Internet
Author: User

Analysis of jsp-c tag usage instance and jsp tag instance

This example describes the c tag usage of jsp. We will share this with you for your reference. The details are as follows:

Core tag Library

It is the core library in JSTL and provides general support for daily tasks, such as displaying and setting variables, reusing a set of projects, test conditions, and other operations (such as importing and redirecting Web content ). Core labels can be divided into four types by function:

1 variable maintenance:

(1) <c: set>: set variable values and object attributes. Syntax:
Copy codeThe Code is as follows: <c: set value = "value" var = "variable name" scope = "variable scope" target = "Object Name" property = "object property name"> </c: set>

Each setting has two methods. To sum up, the four forms of <c: set> are as follows:

A. Set JSP variables using tag Properties
Copy codeThe Code is as follows: <c: set value = "value" var = "variable name" scope = "scope"/>

B. Use the TAG body to set JSP Variables
Copy codeThe Code is as follows: <c: set var = "variable name" scope = "scope"> tag content </c: set>

C. Set object Properties Using tag Properties
Copy codeThe Code is as follows: <c: set value = "variable name" target = "Object Name" property = "object property name"/>

D. Set object attributes using the TAG body
Copy codeThe Code is as follows: <c: set target = "Object Name" property = "Scope"> tag content </set>

(2) <c: remove>: delete a variable within the specified scope. Syntax:
Copy codeThe Code is as follows: <c: remove var = "variable name" scope = "scope"/>

2. Process Control: it can be divided into condition labels and iteration labels.

Condition Tag: <c: if> <c: choose> <c: when> <c: otherwise>

(1) <c: if>: it is the same as the if statement in Java, but cannot implement the else function.

<C: if> A tag has two syntax forms: whether the tag contains a tag or not.
No tag body:
Copy codeThe Code is as follows: <c: if test = "test condition" var = "variable name" [scope = "scope"]/>
TAG body:

<C: if test = "test condition" var = "variable name" [scope = "scope"]> label body </c: if>

<C: if>
Copy codeThe Code is as follows: <c: if test = "$ {user. visitCount! = 0} "> welcome </c: if>

(2) <c: choose> <c: when> <c: otherwise>
<C: when> <c: otherwise> cannot be used independently. It can only be used as a sub-tag of <c: choose>. These three labels are combined to implement the switch statement function in Java. Syntax:

<C: choose> <c: when test = "$ {user. class = 'guest '} "> tag body 1 </c: when> <c: when test =" $ {user. class = 'vip '} "> tag body 2 </c: when> <c: otherwise> tag body 3 </c: otherwise> </c: choose>

Iteration Tag: <c: forEach> <c: forTokens>

(1) <c: forEach>: Used to traverse an object set.

<C: forEach var = "variable name" items = "set" varStatus = "traversal status name" begin = "begin" end = "end" step = "step"> label body </c: forEach>

(2) <c: forTokens>: Used to traverse a string and return a word in the string each time the result is traversed.

<C: forTokens items = "string" delims = "delimiter" var = "variable name" varStatus = "traversal status name" begin = "begin" end = "end" step = "sep"> tag body </c: forTokens>

3. URL Management

(1) <c: url>: used to encode the URL address.

TAG body:

<C: url value = "URL" context = "path" var = "variable name" scope = "scope"> tag body </c: url>

The following code:

<C: url value = "http: // localhost: 8080/el/index. jsp "var =" NewURL "> <c: param name =" name "value =" zero "/> <c: param name = "age" value = "28"/> </c: url> <a href = "$ {NewURL}"> click me </a>

Generated URL: http: // localhost: 8080/el/index. jsp? Name = zero & age = 28

Label-free body: used to edit the context URL.
Copy codeThe Code is as follows: <c: url value = "URL" context = "path" var = "variable name" scope = "scope"/>

The following code:
Copy codeThe Code is as follows: <c: url value = "/logon. jsp"> logon </c: url>
If the current path is el, the output is/el/logon. jsp.

(2) <c: import>: Introduce URL resources to the current JSP page (which can be resources on the remote Program site ). The Include command and include action cannot introduce resources other than Web programs to JSP pages. The introduced resources must be in the current Web program.

Syntax introduced with a String object:

<C: import url = "Address" context = "context path" var = "variable name" scope = "scope" charEncoding = "Character Set"> label body usage <c: param> </c: import>

The following code introduces external resources to the current JSP page.

<C: import url = "http://www.hao123.com" var = "myurl" charEncoding = "gb2312"> </c: import> <a href = "$ {myurl}"> address </a>

Syntax imported as a Reader object:

<C: import url = "Address" context = "context path" varReader = "variable name" scope = "scope" charEncoding = "Character Set"> the TAG body uses other action elements </c: import>

(3) <c: redirect>: used for HTTP redirection.

No tag body:
Copy codeThe Code is as follows: <c: redirect url = "Address" context = "context path"/>

TAG body:

<C: redirect url = "Address" context = "context path"> <c: param/> label </c: redirect>

(4) <c: param>: it can only be embedded in the <c: url>, <c: import>, and <c: redirect> labels as sub-elements. This label is mainly used to set the parameters to be passed in the URL.

No tag body:
Copy codeThe Code is as follows: <c: param name = "name" value = "value"/>

TAG body:

<C: param name = "name" value = "value"> tag body </c: param>

4. Other labels: <c: out> and <c: catch>.

(1) <c: out>: display the variable content on the JSP page.

No tag body:
Copy codeThe Code is as follows: <c: out value = "value" escapeXml = "{true | false}" default = "default"/>

TAG body:

<C: out value = "value" escapeXml = "{true | false}" default = "default"> tag body </c: out>

Where:

Default: Specifies the value to be output when the value is null.
EscapeXml: Used to set whether to escape "<", ">", "&", "'", ", and.
The default value of escapeXml is true, indicating conversion.
"<" Convert to "<"
">" Convert to ">"
"&" "&"
"'" "'"
""""
(2) <c: catch>: used to handle JSP page errors.

If a JSP page error occurs, you can set the page command attribute to go to the error handling page. <C: catch> tags are complementary to handling such errors. The processing method is to embed JSP code snippets that may cause exceptions into the TAG body, and then use the var attribute to receive exceptions thrown by the TAG body.

<C: catch var = "variable name"> nested action </c: catch>

Usage:

<C: catch var = "myexception"> nested action </c: catch> <c: if test = "$ {myexception! = Null} "> content </c: if>

Finally, we will discuss the version issues.

<?xml version="1.0" encoding="ISO-8859-1"?><web-app 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 version="2.4">

Jstl. jar and standard. jar are required for version 2.4. Note the version issue. Different versions are not compatible. ,
A JSP exception occurs during application deployment and running. When JSTL library is used: According to TLD or attribute directive in tag file, attribute value does not accept any expressions, it may be because the JSP2.0 version is used, and the standby version of The JSTL core library (RT Library) is not used. There are two solutions:

1. If you do not want to use the web-app_2_4.xsd and jstl1.1, you can modify it in either of the following ways:

1). modify web. xml.
Copy codeThe Code is as follows: <web-app 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; version =" 2.4 ">

Changed to Version 2.3
Copy codeThe Code is as follows: <! DOCTYPE web-app PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 //" http://java.sun.com/dtd/web-app_2_3.dtd ">
<Web-app>

2). Use the JSTL core RT Library

The JSTL core library has two types of taglib pseudocommands. The RT library relies on the traditional JSP request-time attribute value instead of EL for implementation (called EL library. JSP2.0 will support EL)

JSP using <% @ taglib uri = http://java.sun.com/jstl/core prefix = "c" %> can be in Version 2.3, not in version 2.4, this is caused by version incompatibility, the servlet version will be introduced later.

As long
Copy codeThe Code is as follows: <% @ taglib uri = "http://java.sun.com/jstl/core" prefix = "c" %>

Change
Copy codeThe Code is as follows: <% @ taglib uri = http://java.sun.com/jstl/core_rt prefix = "c" %>

2: If you want to use jstl1.1 (recommended), follow the steps below to modify it.

Jstl has the difference between 1.0 and 1.1. We recommend that you use EL in version 1.1,
To use jstl1.1, you only need
1.0
Copy codeThe Code is as follows: <% @ taglib uri = "http://java.sun.com/jstl/core" prefix = "c" %>
Replace:
Copy codeThe Code is as follows: <% @ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

I hope this article will help you with JSP program design.

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.