Goto: C Tag of JSP

Source: Internet
Author: User
Tags variable scope

Http://www.gbsou.com/2009/10/12/1028.htmljsp label C label

Core Tag Library

It is the core library in Jstl, providing common support for everyday tasks, such as displaying and setting variables, reusing a set of items, testing conditions, and other actions such as importing and redirecting Web content. Core tags can be divided into 4 types by function:

1 Variable Maintenance:

(1) <c:set>: Sets variable values and object properties. The syntax 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 ways, summed up in 4 forms of,<c:set>, as follows:

A. Setting JSP variables with tag properties

<c:set value= "value" var= "variable name" scope= "Scope"/>

B. Setting a JSP variable with a tag body

<c:set var= "variable name" scope= "Scope" > Tagged content </c:set>

C. Setting object properties with Tag properties

<c:set value= "variable name" target= "Object Name" property= "object property name"/>

D. Setting object properties using the marker body

<c:set target= "Object name" property= "Scope" > Tagged content </set>

(2) <c:remove>: Deletes a variable at the specified scope. The syntax is as follows:

<c:remove var= "variable name" scope= "Scope"/>

2 Process Control: divided into conditional tags and iteration labels.

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

(1) <c:if>: The use of the IF statement in the Java language is the same as, but does not implement else functionality.

The <c:if> tag has two grammatical forms, which are distinguished by the unmarked body.

No label body:

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

Tagged Body:

<c:if test= "test condition" var= "variable name" [scope= "Scope"]>

Label body

</c:if>

<c:if> with tagged body

<c:if test= "${user.visitcount!=0}" > Welcome </c:if>

(2) <c:choose> <c:when> <c:otherwise>

<c:when> <c:otherwise> cannot be used alone and can only be used as a sub-label for <c:choose>. These three tags combine to implement the switch statement functionality in Java. The syntax is as follows:

<c:choose>

<c:when test= "${user.class== ' guest '}" >

Label Body 1

</c:when>

<c:when test= "${user.class== ' VIP '}" >

Label Body 2

</c:when>

<c:otherwise>

Label Body 3

</c:otherwise>

</c:choose>

Iterate tags:<c:foreach> <c:forTokens>

(1) <c:foreach>: Used to traverse a collection of objects.

<c:foreach var= "variable name" items= "collection" varstatus= "Traversal state name"

begin= "Begin" end= "End" step= "Step" >

Label body

</c:forEach>

(2) <c:fortokens>: Used to iterate through a string, and each time a traversal result returns a word in a string.

<c:fortokens items= "string" delims= "delimiter" var= "variable name"

varstatus= "Traversal state name" begin= "Begin" end= "End" step= "Sep" >

Label body

</c:forTokens>

3 URL Management

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

Tagged Body:

<c:url value= "url" context= "path" var= "variable name" scope= "Scope" >

Label 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= "/>"

</c:url>

<a href= "${newurl}" > Point me </a>

The generated url:http://localhost:8080/el/index.jsp?name=zero&age=28

No tag body: used primarily for editing context URLs.

<c:url value= "url" context= "path" var= "variable name" scope= "Scope"/>

The following code:

<c:url value= "/logon.jsp" > Login </c:url>

If the current path is El, the output is:/el/logon.jsp

(2) <c:import>: Introduces a URL resource to the current JSP page (which can be a resource on a remote program site). The include directive and the include action do not introduce resources other than web programs to JSP pages, and the imported resources must be in the current Web program.

Syntax introduced as a string object:

<c:import url= "Address" context= "Context path" var= "variable name"

Scope= "Scope" charencoding= "character Set" >

Label Body Use <c:param>

</c:import>

The following code: introduce an external resource into the current JSP page.

<c:import url= "http://www.hao123.com" var= "Myurl" charencoding= "gb2312" >

</c:import>

<a href= "${myurl}" > Address </a>

Syntax to import with the reader object:

<c:import url= "Address" context= "Context path" varreader= "variable name"

Scope= "Scope" charencoding= "character Set" >

Tag body uses other action elements

</c:import>

(3) <c:redirect>: For HTTP redirection.

No label body:

<c:redirect url= "Address" context= "Context path"/>

Tagged Body:

<c:redirect url= "Address" context= "Contextual path" >

<c:param/> tags

</c:redirect>

(4) <c:param>: can only be embedded in the <c:url>,, <c:import>, <c:redirect> tags as child elements to use. This label is used primarily to set the parameters that will be passed in the URL.

No label body:

<c:param name= "name" value= "value"/>

Tagged Body:

<c:param name= "name" value= "Value" >

Label body

</c:param>

4 other tags:<c:out>, <c:catch>.

(1) <c:out>: Displays variable contents in a JSP page.

No label body:

<c:out value= "value" escapexml= "{true|false}" default= "Default"/>

Tagged Body:

<c:out value= "value" escapexml= "{true|false}" default= "Default Value" >

Label body

</c:out>

which

Default: Used to specify the value that should be output when the value of value is null.

EscapeXML: Used to set whether <, >, &, ', ', ', ', ', ', or ' are escaped.

The EscapeXML default is true, which indicates that the conversion occurred.

"<" converted to "&lt"

">" Converted to "&gt"

"&" converted to "&amp"

"'" Converted to "& #039"

"" "Convert to" & #034 "

(2) <c:catch>: Used to handle JSP page errors.

If the JSP page is faulted, you can go to the error handling page by setting the page directive property. The <c:catch> tag is a supplement to this error handling. It is handled by embedding the JSP code fragment that may have an exception in the tag body, and then using the var attribute to get the exception that is thrown by the receiving tag body.

<c:catch var= "variable name" >

Nested actions

</c:catch>

How to use:

<c:catch var= "MyException" >

Nested actions

</c:catch>

<c:if test= "${myexception!=null}" >

Content

</c:if>

Finally, a discussion of 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 ">

Version 2.4 requires Jstl.jar and Standard.jar, but also attention to version issues, different versions are incompatible. 、

JSP exception occurs when the application deployment is running, when using the JSTL library: According to TLD or attribute directive in tag file, attribute value does no accept any expressions, perhaps because of the use of the JSP2.0 version, while not using the backup version of the JSTL Core library (RT library), there are two ways to handle this:

1. If you do not want to use web-app_2_4.xsd and jstl1.1 then you can modify it in the following two ways

1). Modify Web. Xml.

<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" >

Change to version 2.3

<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD ">

<web-app>

2). Using the JSTL Core RT Library

There are two taglib pseudo-directives for the JSTL Core library, where the RT library is dependent on the property value of the JSP's traditional request, rather than relying on El to implement it (called the El Library. JSP2.0 will support EL)

JSP in the use of <%@ taglib uri=http://java.sun.com/jstl/core prefix= "C"%> in the 2.3 version can be, in 2.4 is not, this is the version incompatibility caused by, The version of the servlet is described later.

As long as the

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

Switch

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

2: If you want to use jstl1.1 (recommended) Then follow the changes, very simple.

Jstl There are 1.0 and 1.1 difference issues, with El Recommendations need to be in the 1.1 version,

Using jstl1.1 only needs to be

The 1.0 is

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

Into:

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

This article link address: JSP label C label reproduced please retain, thank you!

Referenced by: http://hi.baidu.com/libaohuimsn/item/4ecd06f178d6dd1dd7ff8c82

Goto: C Tag of JSP

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.