Review JSP learning notes -- JSTL tag library and learning notes -- jstl
In the past, when I wrote jsp files, it was a little resistant to the jstl tag library, because I felt that the method of embedding java code was almost always unfavorable and there was no need to use the tag library. However, I have learned a bit about this review, it is very useful to find out that it saves a lot of trouble. JSTL is an extension of el expressions and a label language, which is easy to use, he does not belong to the jsp built-in labels. He needs to export the package and specify the tag library before using it. In Myeclipse, he has built-in jstl jar package, so he does not need to export the package.
JSTL has four tag libraries, and only one core is commonly used. The other is rarely used. Before using JSTL, you must first import the tag library. The Code is as follows:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
The following describes commonly used labels. because most of them are core library labels, they are generally called c labels:
Example:
<C: out value = "aaa"/> output string aaa <c: out value = "$ {aaa"/> output domain attribute aaa, it is the same as $ {aaa}. <c: out value = "$ {aaa}" default = "xxx"/> If $ {aaa} does not exist, the output xxx string <c: out value = "$ {aaa}" escapeXml = "true"/> If $ {aaa} contains special characters, escape it. This prevents javascript attacks.
- <C: set>: Set (attributes of the created domain)
For example:
<c:set var="name" value="zhangSan" scope="request"/><% request.setAttribute("name"); %>
The upper and lower lines of code have the same effect. They both create a name attribute in the request field and the value is "zhangSan ".
- <C: remove>: Delete domain Variables
<C: remove var = "a"/> delete a domain attribute named a <c: remove var = "a" scope = "page"/> Delete the domain attribute named a in the page domain
- <C: url>: Output a url or save the url to a domain
<C: url value = "/AServlet"/> output URL:/project name/AServlet <c: url value = "/AServlet" var = "url" scope = "page"/> saves the generated url to the page domain without outputting <c: url value = "/AServlet">: Output URL:/project name/AServlet? Username = % xx. Three of them are URL encoded. <c: param name = "username" value = "zhangsan"/> </c: url/>
- <C: if>:Corresponding to the if statement in Java
- <C: choose>:Corresponding to if/else in java statements
Example:
<C: set var = "score" value = "$ {param. score} "/> <c: choose> <c: when test =" $ {score> 100 | score <0} "> incorrect score: $ {score} </c: when> <c: when test = "$ {score >=90}"> A level </c: when> <c: when test = "$ {score >=80}"> Level B </c: when> <c: when test = "$ {score >=70}"> level C </c: when> <c: when test = "$ {score >=60}"> D level </c: when> <c: otherwise> E level </c: otherwise> </c: choose>
- <C: forEach>: Used to traverse the array cyclically, set or cycle by counting! Corresponding java for Loop
--> Alas
--> Good
--> This crane
Note that the forEach tag has another attribute: varStatus, which is used to specify the variable name that receives the "loop status", for example: <forEach varStatus = ""... /> In this case, you can use the vs variable to obtain the Loop State. Its Attributes are as follows:
In the above example, we traverse all the elements of the array. What should we do if we only want to retrieve the first element of the array "?
<c:forEach items="${array }" var="i" varStatus="index"> <c:if test="${index.first }"> --->${i } </c:if></c:forEach>
In this way, the output result is changed:
---> Alas