Java Study Notes-JSTL (37), study notes jstl
In the previous development, we found that various JSP script fragments often cannot be avoided in JSP. As a result, page art Engineers cannot maintain them well. Therefore, today's technology can help developers quickly reduce the appearance of JSP scripts.
JSTL Introduction
JSTL is the Standard Tag library of Jsp. This technology provides many labels for encapsulating some basic business logic commonly used in JSP.
The main categories are as follows:
Core Database: it encapsulates some basic core business logic.
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
Formatting and internationalization: Mainly encapsulates data formatting and internationalization services. For example, format the date.
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "f" %>
XML library: Mainly encapsulates some business logic for parsing XML data.
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/xml" prefix = "xml" %>
SQL database: it encapsulates the business logic of database operations.
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/ SQL" prefix = "SQL" %>
Function library: the main character encapsulates common functions. For example, String
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "fun" %>
1 JSTL Introduction
<% @ 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 = "<% = 23 = 23%>"> </c: out>
The preceding labels can avoid the following output statements.
<%
Out. println ("")
%>
Core library tag
1. c: out
<C: out output tag value = "" output content, it can be an output expression <% = %> default = "" default output value: escapeXml = ""> whether to output data in xml format </c: out>
Example:
<C: out value = '<% = pageContext. getAttribute ("name", PageContext. REQUEST_SCOPE) %> 'escapexml = "false"> </c: out>
2. c: set
<C: set sets the property var = "" attribute name value = "" attribute value scope = "" specified domain object target = "" specified set Name property = ""> set attribute name </c: set>
Example: <c: set var = "psw" value = "root" scope = "page"> </c: set> <br/> <c: out value = '<% = 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 Delete the attribute var = "" specified attribute name scope = ""/> specified domain name
4. c: catch
<C: catch var = ""> à handle exceptions and specify the attributes of the exception object stored in the page field.
</C: catch>
Example: <c: catch var = "error"> <% = 12/0%> </c: catch> the exception message is: <c: out value = '<% = (Exception) pageContext. getAttribute ("error", PageContext. PAGE_SCOPE )). getMessage () %> '> </c: out>
5. c: if judgment statement
<C: if test = ""> à specified condition statement
À if the execution label body content is set up
</C: if>
Example: <c: if test = '<% = pageContext. getAttribute ("list", PageContext. SESSION_SCOPE) = null %> '> <font color = "red"> the data is empty! </Font> <br/> </c: if>
6. if... Selse
<C: choose> select <c: when test = ""> specify a Condition Statement </c: when> <c: otherwise> the condition is not true </c: otherwise> </c: choose>
Example: <c: choose> <c: when test = '<% = "jack ". equals ("jack") & "root ". equals ("root2") %> '> <c: out value = "welcome"> </c: out> </c: when> <c: otherwise> <c: out value = "register"> </c: out> </c: otherwise> </c: choose>
7. c: forEach loop (important)
<C: forEach loop begin = "" loop start value end = "" loop end value step = "" loop step var = "" place the specified attribute name of the cyclic data in the page domain items = "" cyclic set data varStatus = ""> cyclic status object </c: forEach>
Example: <c: forEach begin = "0" end = "10" var = "I"> <c: out value = '<% = pageContext. getAttribute ("I", PageContext. PAGE_SCOPE) %> '> </c: out> <br/> </c: forEach>
Example:
<% List<String> list = new ArrayList<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:out value='<%= 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:out value='<%= 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:out value='<%= pageContext.getAttribute("str",PageContext.PAGE_SCOPE)%>'></c:out> </td> </tr> </c:forEach> </table>
8. c: url
<C: url var = "" specify the attribute name value = "" specify the attribute value scope = "" specify the domain context = ""> specify the 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>
10. 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>
11. c: import tag
Introduction page <c: import url = "test. jsp"> </c: import>