JSP reference JSTL core Tag Library

Source: Internet
Author: User
Tags control label

I. Quoting JSTL

1, the introduction of JSTL can make JSP code in <%%> and other code disappears, and then combined with El expression, will be more convenient and beautiful.

2, each set of framework (not learning, such as STRUTS,SPRINGMVC, etc.) have their own tag library, then Jstl can be as public, general, in the various frameworks.

Operation:

First step: Download the JSTL package

: http://tomcat.apache.org/taglibs/

Step two, create a new JSP page

 JSTL Basic applications:

  1. Core Tag library: including basic language function, variable operation, process control and so on.

  2. Internationalization and Formatting Tags Library: internationalization and formatting operations, such as formatting time, and so on.

  3.SQL Tag Library: used primarily for storage operations on relational databases.

  4.XML Tag Library: used primarily for data manipulation in XML format.

  5. Function Tag Library: primarily defines a library of functions for string manipulation, running in an El expression.

  In the above five kinds of tag libraries, commonly used have core tag library, format tag library and function tag library, in the following content we will explain to you.

II. Core Tag Library 1. Multi-purpose Core tags

1.1 <c:out> for display

The <c:out> tag is the most commonly used label for displaying data in a JSP.

  The newfile.jsp code is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix = "C"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Operation Result:

1.2 <c:set> tags for assigning values

<c:set> tags are used to work on assigning values to variable attributes in variables or JavaBean.

  The newfile.jsp code is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix = "C"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >${onestring} // The example assigns a variable named "onestring" to "This is Andy", which is scoped to page. </body>

Operation Result:

1.3 <C:remove> Tags for deletion

<C:remove> tags are used to delete variables that exist in scope.

  The newfile.jsp code is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix = "C"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >${sessionscope.samplevalue}//  The example will exist in the session named "Samplevalue" the variable is deleted. When the next El expression displays the variable, the variable no longer exists. </body>

1.4 <C:catch> Tags for exception capture

<C:catch> tags allow exceptions to be caught in JSP also polygons. It contains a var attribute, which is a variable that describes the exception.

    The newfile.jsp code is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix = "C"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >catch>$ {param.samplesinglevalue[9]==3}</c:catch>${err}// when "${ Param.samplesingvalue[9]==3} "When an expression has an exception, you can get the contents of the exception from the var attribute" err " , which usually determines whether" err "is null to determine the error message. </body>

Operation Result:

2. Condition Control Label

2.1 <c:if> Tags for judging

<c:if> tags are used for simple conditional statements.

Let's look at an example:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix = "C"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >if test=" ${ PARAMVALUE.SAMPLEVALUE[2]==12} "var=" visits ">    12</c:if><br>${visits}
Determines whether the control content labeled "2" is "12" in the incoming control array parameter for request submission, if 12
Displays "It is 12". The result of the decision is saved in the "visits" variable in the page range. </body>

2.2 Complex judgments <c:choose>, <c:when>, <c:otherwise> tags

These three tags implement complex conditional judgment statements, similar to "if,elseif" conditional statements.

1.<c:choose> tags have no attributes and can be considered as parent tags,<c:when>, <c:otherwise>

will be used as its child label.

The 2.<c:when> tag is equivalent to an "if" statement, which contains a test property that represents the condition that needs to be judged.

The 3.<c:otherwise> tag has no attributes and is equivalent to the "else" statement.

Here is an example of a complex conditional statement:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "UTF-8"%><%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > not13,it is 11</c:when><c:when test= "${paramvalues.samplevalue[2]==12}" > not13,it is 12</c:when><c:when test= "${paramvalues.samplevalue[2]==13}" > not12,it is 13</c:when><c:otherwise> not11, 12, 13</c:otherwise></c:choose>
Determine the incoming control array parameter for request submission, subscript "2" control whether the content is "11" or "12" or "13", and according to the results of the decision to display their own statements, if not the display "not 11, 12," </body></ Html>

Operation Result:

To be Continued ...

JSP reference JSTL core Tag Library

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.