Java Learning notes 13:spring JSTL core Tag Library use

Source: Internet
Author: User

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= "string", for example:

<body>
  <c:out value= "< Data objects to display (no escape characters are used) >" escapexml= "true" default= "Default" ></c:out>< br/>
  <c:out value= "< Data objects to display (using escape characters) >" escapexml= "false" default= "default" ></c:out><br/ >
  <c:out value= "${null}" Escapexml= "false" > uses an expression that evaluates to NULL, output the default value </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>

<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

<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, as illustrated in the following example:

View Code <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%> <% @page import= "java.util.List"%> <% @page Imp Ort= "Java.util.ArrayList"%> <%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>  

Display effect:

8.<c:fortokens> is used to browse strings and intercept strings based on the specified string
Syntax: <c:fortokens items= "Stringoftokens" delims= "delimiters" [var= "name" begin= "Begin" end= "End" step= "Len" varstatus= "Statusname"]></c:fortokens>

Let's look at an example:

View Code <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%> <%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/j" Stl/core "%>  

Show Results:

9.URL Operation label

(1) <c:import> include other static or dynamic files to the JSP page. The difference with <jsp:include> is that the latter can contain only files from the same Web application, including files in other Web applications, or even resources on the network.

Syntax: <c:import url= "url" [context= "context"] [value= "value"] [scope= ...] [charencoding= "Encoding"]></C: Import>

<c:import url= "url" varreader= "name" [context= "Context"][charencoding= "encoding"]></c:import>

Look at an example: View Code

View Code <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%> <%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/j"
     Stl/core "%>  

Show Results:

The URL path has an absolute path and a relative path. Relative path: <c:import url= "A.txt"/> Then, a.txt must be placed in the same file directory as the current file. If you start with "/", the representation resides in the root directory of the application, such as the root folder of the Tomcat application as WebApps. Import the B.txt under this folder: <c:import url= "/b.txt" >. If you want to access other Web applications in the WebApps Management folder, use the context attribute. For example, to visit the index.jsp under Demoproj, then: <c:import url= "/index.jsp" context= "/demoproj"/>.

(2) <c:redirect> the label is used to implement the redirection of the request. For example, the user name and password entered by the user are validated and, if unsuccessful, redirected to the login page. or implement the interface between different modules of Web application

Syntax: <c:redirect url= "url" [context= "Context"]/>

Or: <c:redirect url= "url" [context= "Context"]>

<c:param name= "name1" value= "value1" >

</c:redirect>

Look at an example:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
2 <%@ taglib prefix= "C" uri= "http://java.sun.com/jsp/ Jstl/core "%>
3 <c:redirect url=" http://127.0.0.1:8080 ">
4     <c:param name=" uname ">lihui </c:param>
5     <c:param name= "password" >11111</c:param>
6 </c:redirect>

After the run, the page jumps to: http://127.0.0.1:8080/?uname=lihui&password=11111

(3) <c:url> is used to dynamically generate a String URL that can be used in conjunction with the previous tag, or you can use HTML <a> tags to experiment with hyperlinks.

Syntax: <c:url value= "Value" [var= "name"] [scope= ...] [context= "Context"]>

<c:param name= "name1" value= "value1" >

</c:url>

Or: <c:url value= "Value" [var= "name"] [scope= ...] [context= "Context"]/>

Look at an example:

View Code 
 <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
 <%@ taglib prefix= "C" uri= "http://" Java.sun.com/jsp/jstl/core "%>
 <c:out value=" url tag usage ></c:out>
 

Show:


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.