JSTL Core Tag Library using

Source: Internet
Author: User
JSTL core Tag library using

JSTL Core Tag Library has a total of 13 tags, functionally divided into 4 categories:

1. Expression control Tags: out, set, remove, catch

2. Process Control Tag: If, choose, when, otherwise

3. Cyclic Tags: forEach, fortokens

4.URL operation tag: import, URL, redirect

When using labels, be sure to include the following code in the header of the JSP file:

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

These labels are described in the following sections:

1. <c:out> to display the contents or results of a data object (string, expression)

Java scripts are used in the following ways: <% out.println ("Hello")%> <% = Expression%>

Use the JSTL tag: <c:out value= "http://www.cnblogs.com/peter1120/archive/2012/07/22/string", for example:

<body>
  <c:out value= "&lt the data object to display (not using the escape character) &gt" escapexml= "true" default= "Default value" ></c:out ><br/>
  <c:out value= "&lt the data object to display (using the escape character) &gt" escapexml= "false" default= "Default value" ></c:o ut><br/>
  <c:out value= "${null}" Escapexml= "false" > The result of the expression used is NULL, the default value is output </c:out><br/ >
</body>

Then the Web page displays the following effect:


2. <c:set> is used to access variables in the JSP scope or JavaBean properties. The following example assumes that there is already a Person.java class file.

<%@ page language= "java" import= "java.util.* pageencoding=" gb2312 "%> <% @page contenttype=" text/html; Charset=utf-8 "%> <jsp:usebean id=" person "class=" Lihui. Person "></jsp:useBean> <% @taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

A total of four syntax formats, the first two are to the JSP range variable assignment, the latter two are to JavaBean variable assignment

The effect is as follows:

3.<c:remove> is primarily used to remove the specified variable from the specified JSP scope. With similar, the following only gives syntax:

<c:remove var= "variable name" [scope= "Page|request|session|application"]></c:remove>


4.<c:catch> is used to handle exceptions generated in JSP pages and to store exception information

<c:catch var= "Name1" >

Code that produces exceptions easily

</c:catch>

If the exception is thrown, the exception information is saved in the variable name1.

5.<c:if>

<c:if test= "Condition 1" var= "name" [scope= "Page|request|session|application"]></c:remove>

Cases:

  <body>
  <c:set value= "Zhao Wu" target= "${person}" property= "name" ></c:set>
  <c:set target= "$ {person} ' property= ' age ' >19</c:set>
  <c:if test= ' ${person.name = = ' Zhao Wu '} ' var= ' name1 ' ></c:if >
  <c:out value= "name1 value: ${name1}" ></c:out><br/>
  <c:if test= "${person.name = = ' Zhao Wu '} "Var=" name2 ></c:if>
  <c:out value= "name2 value: ${name2}" ></c:out>
  </body>

Effect:

6. <c:choose> <c:when> <c:otherwise> Three tags are usually nested, the first tag is at the outermost layer, and the last label can only be used once in the nesting

Cases:

    <c:set var= "Score" >85</c:set>
    <c:choose>
    <c:when test= "${score>=90}" >
    Your grades are excellent.
    </c:when>
    <c:when test= "${score>=70&&score<90}" >
    your grades are good!
    </c:when>
    <c:when test= "${score>60&&score<70}" >
    your grades are passed
    </c:when>
    <c:otherwise>
    Sorry, you didn't pass the exam.
    </c:otherwise>
    </c:choose>

7.<c:foreach>

Syntax: <c:foreach var= "name" items= "Collection" varstatus= "Statusname" begin= "Begin" end= "End" step= "Step" ></c: Foreach>

The label traverses the elements in the collection Collection based on the loop condition. var is used to store the elements that are fetched from the collection, items specify the collection to traverse, and varstatus the information that is used to hold the elements in the collection. Varstatus has a total of 4 state attributes, which are described in the following example: (Varstatus can achieve interlacing. ) View Code

 1 <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%> 2 <% @page import= "Java.util.List"%> 3 <% @page import= " Java.util.ArrayList "%> 4 <%@ taglib prefix=" C "uri=" Http://java.sun.com/jsp/jstl/core "%> 5 
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.