JSP C Tag Usage example Analysis _jsp programming

Source: Internet
Author: User
Tags character set error handling

This article illustrates the C tag usage of JSP. Share to everyone for your reference, specific as follows:

Core Tag Library

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

1 Variable Maintenance:

(1) <c:set>: Set Variable values and object properties. The syntax is as follows:

Copy Code code as follows:
<c:set value= "value var=" variable name "scope=" variable's scope "target=" Object name property= "object property name" ></c:set>

Each setting has two ways to sum up the 4 forms of,<c:set>, as follows:

A. Setting a JSP variable with a tag property

Copy Code code as follows:
<c:set value= "value" var= "variable name" scope= "Scope"/>

B. Setting a JSP variable with a tag body

Copy Code code as follows:
<c:set var= "variable name" scope= "Scope" > tag content </c:set>

C. Setting object properties with Tag properties

Copy Code code as follows:
<c:set value= "variable name" target= "Object Name" property= "object property name"/>

D. Setting object properties with a tag body

Copy Code code as follows:
<c:set target= "Object name" property= "Scope" > tag content </set>

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

Copy Code code as follows:
<c:remove var= "variable name" scope= "Scope"/>

2 Process Control: It is divided into conditional label and iterative label.

Conditional label:<c:if> <c:choose> <c:when> <c:otherwise>

(1) <c:if>: Similar to the use of IF statements in the Java language, but cannot implement else functions.

The <c:if> label has two grammatical forms, which are distinguished by the use of unmarked bodies.
No label body:

Copy Code code as follows:
<c:if test= "test condition" var= "variable name" [scope= scope]/>

Has the label body:

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

<c:if> with tagged body

Copy Code code 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 alone and can only be used as a child label for <c:choose>. These three tags combine to implement the functionality of the switch statements in Java. The syntax is as follows:

  <c:choose>
  <c:when test= "${user.class== ' guest '}" >
    tag body 1
  </c:when>
  <c:when test= "${user.class== ' VIP '}" >
    label body 2
  </c:when>  
  <c:otherwise>
    label Body 3
  </c:o Therwise> 
  </c:choose>

Iterative label:<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 traverse a string, and each traversal result returns a word in the 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 URL addresses.

Has the label 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= "Zero"/ >
   <c:param name= "age" value= "/>
</c:url>
<a href=" ${newurl} "> Dot me </a>

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

No label body: used primarily for editing context URLs.

Copy Code code as follows:
<c:url value= "url" context= "path" var= "variable name" scope= "Scope"/>

The following code:

Copy Code code as follows:
<c:url value= "/logon.jsp" > Login </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 (can be resources on a remote program site). Include directives and include actions cannot introduce resources other than web programs to JSP pages, and the resources introduced 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: introducing external resources into 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" >
    tag body uses other action elements
</c:import>

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

No label body:

Copy Code code as follows:
<c:redirect url= "Address" context= "Context path"/>

Has the label body:

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

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

No label body:

Copy Code code as follows:
<c:param name= "name" value= "value"/>

Has the label body:

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

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

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

No label body:

Copy Code code as follows:
<c:out value= "value" escapexml= "{true|false}" default= "Default value"/>

Has the label body:

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

which

Default: Specifies the value that should be output when value is null.
EscapeXML: Used to set whether to escape "<", ">", "&", "" "," "," "," and "".
The EscapeXML defaults to true to indicate that the conversion occurred.
"<" into "<"
">" into ">"
"&" into "&"
"'" Into ""
"" To "" "
(2) <c:catch>: For handling JSP page errors.

If there is an error in the JSP page, you can go to the error-handling page by setting the page directive property. <c:catch> tags are a complement to this error handling. It is handled by embedding a snippet of JSP code that might have an exception in the tag body, and then using the Var property to receive the exception thrown by the tag body.

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

How to use:

<c:catch var= "MyException" >
   nested Actions
</c:catch>
<c:if test= "${myexception!=null}" >
  content
</c:if>

Finally on the issue of version of the discussion

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

The 2.4 version requires Jstl.jar and Standard.jar, as well as versioning issues, and different versions are not compatible. 、
JSP exception occurs when application deployment runs, when using the JSTL library: according to TLD or attributes Directive in tag file, attribute value does not accept any expressions, perhaps because of the use of the JSP2.0 version, while not using an alternate 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.

Copy Code code 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" >

Instead of the 2.3 version of

Copy Code code as follows:
<! 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 JSTL Core RT Library

The JSTL Core Library has two taglib pseudo directives, in which the RT library relies on the JSP's traditional request-time attribute value, 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 incompatible version, The version of the servlet will be introduced later.

As long as the

Copy Code code as follows:
<%@ taglib uri= "Http://java.sun.com/jstl/core" prefix= "C"%>

To

Copy Code code as follows:
<%@ 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 differences in the problem, with El Recommendations need to be in the 1.1 version,
Using jstl1.1 you only need to add
1.0 is for

Copy Code code as follows:
<%@ taglib uri= "Http://java.sun.com/jstl/core" prefix= "C"%>

Into:
Copy Code code as follows:
<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>

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

Related Article

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.