JSTL Core Tag Library use

Source: Internet
Author: User
Tags control label

JSTL Core Tag Library use

There are 13 tags in the JSTL core tag library, functionally divided into 4 categories:

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

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

3. Loop Label: ForEach, Fortokens

4.URL action tag: import, URL, redirect

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

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

These labels are described separately below:

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= "&lt data object to display (not using escape character) &gt escapexml=" true "default=" Default "></c:out><br/>
<c:out value= "&lt data object to display (using escape character) &gt" escapexml= "false" default= "default" ></c:out><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 page display effect is:

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

<%@ 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" >
<title>jstl Test </title>

<body>
<c:set value= "Zhang San" var= "name1" scope= "Session" ></c:set>
<c:set var= "Name2" scope= "session" > John Doe </c:set>
<c:set value= "Zhao Wu" target= "${person}" property= "name" ></c:set>
<c:set target= "${person}" property= "Age" >19</c:set>
<li> the value obtained from the session:${sessionscope.name1}</li>
<li> the value obtained from the session:${sessionscope.name2}</li>
<li> get the name value of the object person from the bean: <c:out value= "${person.name}" ></c:out></li>
<li> gets the age value of the object person from the bean: <c:out value= "${person.age}" ></c:out></li>
</body>

A total of four syntax formats, the first two are to give the scope of the JSP variable assignment, the latter two is to assign a value to the JavaBean variable

The effect is as follows:

3.<c:remove> is used primarily to remove specified variables from the specified JSP scope. Using a similar, only syntax is given below:

<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 easily generates exceptions

</c:catch>

If an exception is thrown, the exception information is stored 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= "Value of name1: ${name1}" ></c:out><br/>
<c:if test= "${person.name = = ' Zhao Wu '}" var= "Name2" ></c:if>
<c:out value= "Value of name2: ${name2}" ></c:out>
</body>

Effect:

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

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 iterates through the elements in the collection Collection based on the loop condition. var is used to store the elements taken out of the collection; items specifies the collection to traverse; Varstatus is used to hold the information for the elements in the collection. Varstatus has 4 state attributes, as explained in the following example:

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 6 7 <title>jstl:--foreach Tag instance </title>
8 9 <body>
<%
List a=new ArrayList ();
A.add ("Beibei");
A.add ("Jingjing");
A.add ("Huan Huan");
A.add ("Yingying");
A.add ("Ni ni");
Request.setattribute ("A", a);
%>
<b><c:out value= "does not specify the iteration of Begin and end:"/></b><br>
<c:foreach var= "Fuwa" items= "${a}" >
&nbsp;<c:out value= "${fuwa}"/><br>
</c:forEach>
<b><c:out value= "Specifies the iteration of Begin and end:"/></b><br>
<c:foreach var= "Fuwa" items= "${a}" begin= "1" end= "3" step= "2" >
&nbsp;<c:out value= "${fuwa}"/><br>
</c:forEach>
<b><c:out value= "Output information for the entire iteration:"/></b><br>
<c:foreach var= "Fuwa" items= "${a}" begin= "3" end= "4" step= "1" varstatus= "S" >
Four properties:<br> &nbsp;<c:out value= "${fuwa}"/>
&nbsp;&nbsp; location, index: <c:out value= "${s.index}"/><br>
Total number of iterations &nbsp;&nbsp;: <c:out value= "${s.count}"/><br>
&nbsp;&nbsp; is the first position: <c:out value= "${s.first}"/><br>
&nbsp;&nbsp; is the last location: <c:out value= "${s.last}"/><br>
</c:forEach>
Panax Notoginseng </body>

Display effect:

8.<c:fortokens> is used to browse strings and to intercept strings based on a 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:

1 <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
2 <%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
3 4 5 <title>jstl:--fortokens Tag Example </title>
6 7 <body>
8 9 <c:out value= "Fortoken instance"/>
Ten <c:fortokens items= "North, Beijing, Huan, Ying, you" delims= "," var= "C1" >
<c:out value= "${c1}" ></c:out>
</c:forTokens>
<br>
<c:fortokens items= "123-4567-8854" delims= "-" var= "T" >
<c:out value= "${t}" ></c:out>
</c:forTokens>
<br>
<c:fortokens items= "1*2*3*4*5*6*7" delims= "*" begin= "1" end= "3"
Var= "n" varstatus= "S" >
Four properties:<br> &nbsp;<c:out value= "${n}"/>
&nbsp;&nbsp; location, index: <c:out value= "${s.index}"/>
<br>
&nbsp;&nbsp; Total iterations: <c:out value= "${s.count}"/>
<br>
Whether &nbsp;&nbsp; is the first position: <c:out value= "${s.first}"/>
<br>
&nbsp;&nbsp; is the last position: <c:out value= "${s.last}"/>
<br>
</c:forTokens>
</body>

Show Results:

9.URL Operation label

(1) <c:import> include other static or dynamic files on the JSP page. The difference from <jsp:include> is that the latter can only contain files from the same web app, which could include files from other Web applications, 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

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 "/", it means that the root directory of the application is stored in the root directory, such as the WebApps of the Tomcat application. Import B.txt under this folder: <c:import url= "/b.txt" >. If you want to access other web apps in the WebApps Management folder, use the context property. For example, to access the index.jsp under Demoproj, then: <c:import url= "/index.jsp" context= "/demoproj"/>.

(2) <c:redirect> This tag is used to implement the redirect of the request. For example, the user name and password entered by the user are validated, and the unsuccessful redirect to the login page. or implement the interface between different modules of the 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:

1 <%@ 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 running, the page jumps to: http://127.0.0.1:8080/?uname=lihui&password=11111

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

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

Show:

JSTL Core Tag Library use

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.