JSP Standard Tag Library

Source: Internet
Author: User
Tags http redirect

JSP Standard Tag Library

The JSP standard tag library (JSP standards Tag Library,jstl) is a custom tag library set that implements common features commonly found in Web applications, and programmers use JSTL tags to avoid scripting in JSP pages.

JSTL Overview

(1), Jstl English full name is "JSP standard tag Library", that is, the meaning of JSP standards tag libraries.

(2), JSTL is a standard specification specified by JCP (javacommnunity Process) and is a set of HTML-like tags.

(3) Jstl provides tools and processes for development such as loops, conditions, database access, XML processing, internationalization, and so on.

(4), currently the latest version is 1.2, is an ongoing development and improvement of the development of source code JSP tag Library, it supports a variety of tags.

Jstl is divided into 5 major categories.

Functional Range

Url Prefix

Core tag libraries (cores)

Http://java.sun.com/jsp/jstl/core C

internationalization/Formatting Tag library (il8n)

Http://java.sun.com/jsp/jstl/fmt Fmt

Database tag library (SQL)

Http://java.sun.com/jsp/jstl/sql Sql

XML tag library (XML)

Http://java.sun.com/jsp/jstl/xml X

Functions Tag Library (Functions

Http://java.sun.com/jsp/jstl/functions Fn

(5), before learning Jstl tag Library, you need to download the jar package Jstl need first. There are two methods of obtaining

1) Download through the official website (http://www.apache.org/dist/jakarta/taglibs/standard), get the Jstl.jar, Standard.jar in the API

2) Use the driver package that comes with MyEclipse. When you create a Web project using MyEclipse, select the JSTL support option.

Core Tag Library

(1), core tag library mainly includes universal label, condition label, iteration label and URL-related label

(2), in the beginning of the JSP file using the core tag library, add code:% @tagliburi = "Http://java.sun.com/jsp/jstl/core" prefix= "C"%

1. Universal Label

<c:out> tags

Used to output the results of an expression to the current JspWriter object. It functions like a JSP expression <%=%> or an El expression ${}.

Request.setattribute ("User", "Lisi");

%>

<c:out value="HelloWorld"/><br>

Hello World ${"Hello World"}<br>

<c:out value="${username}"default="xxxx"></c:out><br>

<c:out value="Hello " escapexml= "false"></c:out ><br>

<c:out value="${username}"> Text content </c:out>

<c:set> tags

<c:set> tags are used to set variables in various domain scopes of a JSP page, or to set properties for a Java.util.Map object or JavaBean object

The value is saved to a variable named name, and the name variable is saved to the selected scope

<c:set value= "Monkey King" var="user"scope="request"/>

${user}

<c:set var="user"scope="request"> Pig </c:set>

${user}

<%

Map map=new HashMap();

Request.setattribute ("map", map);

%>

<c:set value= "Tang Zeng " property="AAA" target="${map}"></c:set>

${MAP.AAA}

<%

personp=New person ();

Request.setattribute ("Person", p);

%>

<c:set value="Jerry" property="name"target="${person}"></c:set>

${person.name}

<c:remove> tags

<c:remove> tags are used to remove variables from a specified domain range in a JSP page

<c:remove var="person"scope="request"/>

<c:out value="${person.name}" default="no person"></c:out>

<c:catch> tags

<c:catch> tags are used to capture exception objects that are nested inside the tag body, and to save exception information to variables

<c:catch var="Myex">

<%

int i=10/0;

%>

</c:catch>

Exception: <c:out value="${myex}"></c:out><br>

Exception reason: <c:out value="${myex.cause}"></c:out><br>

Exception message: <c:out value="${myex.message}"></c:out><br>

Exception stack track: <c:out value="StackTrace"></c:out><br>

Condition label

<c:if> tags

<c:if> tags are used to make conditional judgments that function like <%if (Boolean) in a JSP {}%>

<c:if test="${username==null}" var="xx"scope="Request">

change user not logged in

</c:if>

${XX}

<c:choose>, <c:when>, <c:otherwise> tags

<c:choose> tags are used to provide context for conditional selection, which must be used with <c:when> and <c:otherwise> tags

<c:when> as a sub-label for <c:choose>,<c:when> has a test property that has a Boolean value, and if the value of test is true, the contents of the <c:when> tag body are executed.

The <c:otherwise> tag has no attributes, it must appear as the last branch of the <c:choose> tag.

<%

Request.setattribute ("Age", 18);

%>

<c:choose>

<c:when test="${age>70}"> Seniors </c:when>

<c:when test="${age<=70&&age>35}"> Middle-aged </c:when>

<c:when test="${age<=35&&age>18}"> Youth </c:when>

<c:when test="${age<=18&&age>0}"> Junior or Child </c:when>

<c:otherwise> Input Errors </c:otherwise>

</c:choose>

Iteration Labels

<c:forEach> Tags

The <c:forEach> tag is used to iterate over a collection that contains multiple objects, repeatedly executing its label body, or repeating iterations for a fixed number of times.

Attention:

The Items property is not necessarily an attribute, but when you do not use the Items property, you must use the Begin and end properties.

Varstatus: Used to hold information about an existing traversal count. For example, if you varstatus= "I", the information is placed in a variable named I, with the I variable having four attribute values, index, count, first, and last.

<c:foreach begin="1"end="Ten" step="1">

HelloWorld

</c:forEach>

<br>

<%

List list=new ArrayList();

list.add ("AA");

list.add ("BB");

List.add ("cc");

List.add ("DD");

list.add ("ee");

list.add ("FF");

list.add ("GG");

list.add ("hh");

List.add ("II");

List.add ("JJ");

Request.setattribute ("list", list);

%>

<table border="1"width=50%>

<c:foreach items="${list}"var="str" begin="0"end="7" step="1" varstatus="status">

<tr class="${(status.count%2!=0)?" Even ': ' Odd '}' >

<td>${str}</td>

<td>${status.index}</td>

<td>${status.first}</td>

<td>${status.last}</td>

<td>${status.count}</td>

</tr>

</c:forEach>

</table>

<c:forTokens> tags

The <c:forTokens> tag is used to browse all the members of a string, whose members are delimited by the definition symbol (delimiters).

<c:fortokens items="this:is:an:example" delims=":" var="token">

${token}

</c:forTokens>

URL-related tags

<c:url> tags

<%

Session.setattribute ("xxx", "xxx");

%>

<c:url value="/index.jsp" var="strURL " scope="request">

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

</c:url>

${strurl}

<a href="${strurl}"> Home </a> <br>

<a href="<c:url value='/index.jsp '/>"> Home </a>

<c:redirect> tags

The <c:redirect> tag is used to send an HTTP redirect response to the user's browser, which is the Sendredirect () in Jstl with Javax.servlet.http.HttpServletResponse The method is functionally equivalent to the markup.

<c:redirect url="/myjsp.jsp"></c:redirect>

<c:param> tags

The role of the <c:param> tag is to add request parameters to a URL that has been seen in the previous <c:url>, <c:redirect>, and <c:import> tags <c:param> The usage.

<c:import url="/myjsp.jsp"var="Secondpage"></c:import>

JSP Standard 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.