The Jstl of JSP

Source: Internet
Author: User
Tags tld

JSTL Overview:
Apache thing that relies on el;
Use Jstl to import Jstl1.2.jar
Four libraries:
Core: Kernel Library
FMT: formatting dates, numbers
SQL: Obsolete
XML: Obsolete
2. Import Tag Library
* Jar Package
* In the JSP page, <% @taglib prefix= "prefix" uri= "path"%>
======================================================
Core Tag Library Common tags (c tag)
1, out and set
<c:out value= "AAA"/>
<c:out value= "${aaa}"/>
<c:out value= "${aaa}" default= "BBB"/>
<c:out value= "${aaa}" default= "BBB" escapexml= "true"/> The default value is true to indicate escape;
<c:set var= "name" value= "value" scope= "Request"/>
var: variable Name value: Variable value scope: Set domain page/request/session/application If you do not declare default to page
2. Remove
<c:remove var= "a"/>
Scope: If you do not write the default delete all domain variable named a value, if specified only delete the specified;
3. URL
Value: Specify a path! He will automatically add the project name in front of the path.
<c:url value= "/index.jsp" > it will output/day13_1/index.jsp
The child label:<c:param> is used to add parameters to the URL, for example:
<c:url value= "/index.jsp" >
<c:param name= "username" value= "Zhangsan"/>
</c:url>
The result is:/day13_1/index.jsp?username=%ed%2c%d3%2d%4f URL encoding

var: Once this attribute is added, the URL is not exported to the page but added to the domain for saving;
Scope: Used with VAR to hold the specified domain of the URL
4. If
<c:if test= "Boolean value" > when the test value is true, execute the following statement
<c:set var= "A" value= "Hello"/>
<c:if test= "${not Empty A}" >
<c:if>
5, choose
<c:choose>
<c:when tedt= "" >...</c:when>
<c:when tedt= "" >...</c:when>
<c:when tedt= "" >...</c:when>
<c:otherwise>.....</c:otherwise>
</c:choose>
6. ForEach
It is used to iterate through arrays and collections, and it can also be counted in the form of loops!
Counting method:
<c:foreach var= "I" begin= "1" end= "step=" 2 "> equivalent to i>=1 i<=10 step=" Step "
${i}<br/>
</c:forEach>
var = loop variable
begin= Setting the loop variable from several beginnings
end= set the loop variable to a few ends
step= Set Step step defaults to 1
To iterate over arrays and collections:
1. Creating an array
<%
string[] str = {"Name", "value"}
Request.setattribute ("arr", str);
%>
2. Iterating through an array
<c:foreach items= "${str}" var= "S" >
${s}<br/>
</c:forEach>
Items: Arrays or collections to traverse
var: define each member to traverse
foreach Loop state:
You can use Varstatus to create loop state variables!
Count: Number of elements currently traversed
Last: Whether the current loop is the final lap
First: The first element, one lap
Index: Current element subscript
Current: Object
<%
arraylist<string>list= new arraylist<string> ();
List.add ("one");
List.add ("both");
List.add ("three");
Pagecontext.setattribute ("list", list);
%>
<c:foreach items= "${list}" var= "Ele" varstatus= "vs" >
${vs.index}${vs.count}${vs.first}${vs.last}${vs.current}
</c:forEach>
FMT Library:
Format Library
Import the/JSP/JSTL/FMT tag library first
1. Formatted date
<%
Date date = new Date ();
Request.setattribute ("date", date);
%>
<fmt:formatdate value= "${requestscope.date}" pattern= "Yyyy-mm-dd HH:MM:SS"/>
Value is a date class variable
pattern is the date format of the output
2. Format numbers
<%
Request.setattribute ("Num1", 3.1415926);
%>
<fmt:number value= "${requestscope.num1}" pattern= "0.00"/>
Pattern default number of decimal places, and default rounding, vacancy will be 0 to fill the position
<fmt:number value= "${requestscope.num1}" pattern= "#.##"/>
The difference is not to fill the position, are rounded;
Custom Tags:
1, Label processing class (the label is also an object, then you need to have a class first)
2. tld file, he is an XML
3, the page import TLD file location <%@ taglib prefix= "" uri= "TLD True Path"%>
* Label Processing class
Early Java EE provides interface Tag, later with a lot of implementation class, in order to operate convenient sun company provided interface Simpletag;
Jsptag is the father of tag and Simpletag.
We're using Simpletag, and here's how.
void Dotag (): This method is called every time the label is executed;
Jsptag getParent (): Returns the parent tag
void Setjspbody (Jspfragment jspbody): Set label body
void Setjspcontext (Jspcontext pc): Set Label Context Object PageContext is Jspcontext's son.
void SetParent (): Set parent tag
These methods are called by Tomcat itself, and the GetParent () method is called by ourselves, but is not used at all;
All setxxxxx methods are called before the Dotag method, so you can use the object that tomcat passed over in Dotag!
* Configure TLD Files:
TLD files are generally placed under Web-inf to ensure security
* Templates can be used in the TLD file of the core tag to borrow
<tag>
<name>mytag1</name>//Specify the name of the label
<tag-class>cn.itcast.tag.MyTag1</tag-class>//Specify Label class location
<body-content>empty</body-content>//Specify label body type, we use empty label here! For example:<br/> himself began to end himself
</tag>
To make it easier for us to use the implementation class Simpletagsupport, this class has automatically saved the stuff that Tomcat passed and provided a Get method

The Jstl of JSP

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.