In the previous development, we will find that it is often impossible to avoid more or less JSP script fragments in the JSP, so that the page artists can not be very good maintenance. So today's technology can help developers quickly reduce the appearance of scripts in JSPs.
JSTL Introduction
Jstl that is the JSP standard tag libraries that is the JSP standards Tag Library. The technology provides a lot of tags for encapsulating some of the basic business logic commonly used in JSPs.
The main categories are as follows:
Core Library: The main package is some of the basic core business logic.
<% @taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>
Formatting and internationalization libraries: The main package is the data format and internationalization of the business. such as: Date formatting.
<% @taglib uri= "http://java.sun.com/jsp/jstl/fmt" prefix= "F"%>
XML Library: The main encapsulation is some business logic that parses XML data.
<% @taglib uri= "Http://java.sun.com/jsp/jstl/xml" prefix= "xml"%>
SQL Library: The business logic that operates the database is primarily encapsulated.
<% @taglib uri= "http://java.sun.com/jsp/jstl/sql" prefix= "SQL"%>
Library: Starring in encapsulated is a common function. such as: String
<% @taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "Fun"%>
1 Jstl Introduction
- Import the Jstl jar package into the Web project. If you create a 5.0 Java EE project, it is automatically introduced.
- If you need to use this sorry library in a JSP page, you must use the TAGLIB directive to introduce to the current page
<% @taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>
2 Experience
<% @taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>
<c:out value= "<%= =%>" ></c:out>
The above label can be used to avoid the following output statements
<%
Out.println ("")
%>
Core Library Tags
1. C:out
<c:out ? output Label value =
"" ? The output can be the output expression <%=%>
default =
Span style= "color: #000000;" > ? Default value for Output escapexml =
>? Whether to output data in XML </c:out ;
Example:
<c:out value='<%=pagecontext.getattribute ("name", Pagecontext.request_ SCOPE)%>' escapexml="false"></c:out >
2. C:set
<c:Set? set a field or a property in a collectionvar=""? Specify the name of the property in the domain value=""? Specify attribute values in a domain scope=""? Specify the domain object target=""? Specify the collection name property="">? The property name of the collection</C:Set>
Example:<c:Set var="PSW"Value="Root"Scope="page"></C:Set><br/> <c: outValue='<%=pagecontext.getattribute ("PSW", Pagecontext.page_scope)%>'></C: out>
Example:
<!--map.put ("qq", "123456789")--
<c:set target= "<%= map%>" property= "qq" value="123456789" ></c:set>
<%= map.get ("QQ")%>
3. C:remove
<c:remove . Remove the attribute var=""in the domain ? Specify the property name scope=""/> ? Specify Domain name
4. C:catch
<c:catch var= "" >à handles the exception, specifying what property the exception object is stored in the page field
</c:catch>
Example:<c:catchvar="error"> 0 %> </c:catch> Exception message is: <c: out value=' <%= ((Exception) pagecontext.getattribute ("error", Pagecontext.page_scope)). GetMessage ()%>' ></c:out >
5. C:if Judgment Statement
<c:if test= "" >à the specified conditional statement
À if the implementation of the tag body content
</c:if>
Example:<c:if test='<%= pagecontext.getattribute ("list", Pagecontext.session_ SCOPE) = = null%>'> <fontcolor= 'red'> data is empty!</font> <br/> </c:if>
6. If...selse
<c:choose>? External selection<c:when test="">? A conditional statement is specified</c:when> <c:otherwise>? Conditions are not tenable</c:otherwise> </c:choose>
Example:<c:choose> <c:when test='<%= "Jack". Equals ("Jack") && "root". Equals ("Root2")%>'> <c: outValue="Welcome"></C: out> </c:when> <c:otherwise> <c: outValue="Register"></C: out> </c:otherwise> </c:choose>
7. C:foreach cycle (emphasis)
<C:foreach? Cycle begin=""? The start value of the Loop end=""? The value of the loop end step=""? The step of the loopvar=""? Place the loop-out data with the specified property name in the page field items=""? Cyclic collection Data Varstatus="">? The state object of the Loop</c:forEach>
Example:<c:foreach begin="0"End="Ten" var="I"> <c: outValue='<%= Pagecontext.getattribute ("i", Pagecontext.page_scope)%>'></C: out><br/> </c:forEach>
Example:
<%List<String> list =NewArraylist<string>(); List.add ("AAAA"); List.add ("bbbb"); List.add ("CCCC"); Session.setattribute ("List", list); %> <!--Pagecontext.setattribute ("Str","AAAA", Pagecontext.page_scope)-<c:foreach items='<%=pagecontext.getattribute ("list", Pagecontext.session_scope)%>' var="Str"> <c: outValue='<%= pagecontext.getattribute ("str", Pagecontext.page_scope)%>'></C: out><br/> </c:forEach>
Example:
<c:foreach items='<%=pagecontext.getattribute ("list", Pagecontext.session_scope)%>' var="Str"Varstatus="Status"> <c:choose> <c:when test="${status.count% 2 = = 0}"> <tr bgcolor="Red"> </c:when> <c:otherwise> <tr bgcolor="Yellow"> </c:otherwise> </c:choose> <td> <c: outValue='<%= pagecontext.getattribute ("str", Pagecontext.page_scope)%>'></C: out> </td> </tr> </c:forEach>
Example:
<table align="Center"Border="1"> <!--pagecontext.setattribute ("Str","AAAA", Pagecontext.page_scope)-<c:foreach items='<%=pagecontext.getattribute ("list", Pagecontext.session_scope)%>' var="Str"Varstatus="Status"> <tr bgcolor='${Status.count% 2 = = 0? "Gray": "Pink"}'> <td> <c: outValue='<%= pagecontext.getattribute ("str", Pagecontext.page_scope)%>'></C: out> </td> </tr> </c:forEach> </table>
8. C:url
<var="" ? Specifies the property's name, value="" ? Specify the value of the property scope="" ? Specify domain Context=""> ? Specify website </c:url>
Example:
<c:url var="index" value="http://www.itcast.cn" scope="page">
<c:param name="name" value=" jiao Ningbo"></c:param>
</c:url>
9. C:redirect Redirection
Example:<c:redirect url="/list" context="/day09_example" ></c:redirect>
Ten. C:fortokens
<c:fortokens items="james,jack,lucy,jnb" delims="," step ="1"var="name"> <c:out value='<%= pagecontext.getattribute ("name", Pagecontext.page_scope)%>' ></c:out ><br/> </c:forTokens>
One-c:import label
Introduction page <c:import url="test.jsp"></c:import>
Java Learning Note-jstl (37)