1. JSTL tag library
- Core Label Library (Key Points of this Chapter)
- International Label Library (later)
- Database Labels (Basically Not Used)
- XML tags (not used at all)
- JSTL function (EL function, later)
1.1 < c: out > label
The <c:out> tag is used to output a paragraph of text content to the "out" reference currently saved by the pageContext object.
Property name | Supporting EL | Attribute type | Attribute description |
value | true | Object | Specify what to output |
escapeXml | true | Bollean | Determine whether special characters such as > < &'"will be converted into HTML codes before being output. The default value is true |
default | true | Object | If the value attribute value is null, output the default value specified by default |
Give an example:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>c:out Label</title> </head> <body> <strong><c:out value="c:out Label:" /></strong><br> <c:out value="${aa}" default="value is null, output default string"/><br> <c:out value="<a href='http://Www.cnblogs.com/'> Blog Garden</a>“ /><br> <c:out value="<a href='http://Www.cnblogs.com/'> Blog Garden</a>“ escapeXml="false"/><br> </body> </html>
The converted HTML source code:
<html> <head> <title>c:out Label</title> </head> <body> <strong>c:out Label:</strong><br> value is null, output default string<br> <a href='http://www.cnblogs.com/'>Blog Garden</a><br> <a href='http://www.cnblogs.com/'>Blog Garden</a><br> </body> </html>
Operation results:
1.2 < c: set > label
The <c:set> tag is used to store an object within a specified domain, or to set the properties of a java.util.Map-type attribute object or a JavaBean object in a Web domain.
Property name | Supporting EL | Attribute type | Attribute description |
value | true | Object | Used to specify attribute values |
var | false | String | Name used to specify the property to be set |
scope | false | String | The web domain used to specify the properties |
target | true | Object | To specify an object to set properties, this object must be a JavaBean object or a java.util.Map object |
property | true | String | Used to specify the name of the property currently to be set for the object |
Give an example:
package bean; public class User { private String username; private String password; public User() { } public User(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
<%@ page import="bean.User" %> <%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>c:set Label</title> </head> <body> <strong>c:set Label:</strong><br> <!-- Setting Common Properties --> <c:set var="data" value="aaaa" scope="request" ></c:set> ${data} <br> <!-- Set up JavaBean Attributes of --> <% User user = new User(); session.setAttribute("user", user); %> <c:set target="${user}" property="username" value="zhangsan" /> <c:set target="${user}" property="password" value="123456" /> username=${user.username}, password=${user.password}<br> <!-- Set up Map Value --> <% Map<String, String> map = new HashMap<>(); request.setAttribute("map", map); %> <c:set target="${map}" property="aaa" value="111" /> ${map.aaa} </body> </html>
Operation results:
1.3 < c: remove > label
The < c: remove > tag is used to delete attributes of various web domains. Grammar:
<c:remove var="varName" scope="page|request|session|application" />
Give an example:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>c:remove Label</title> </head> <body> <strong>c:remove Label:" </strong><br> <c:set var="data1" value="aaaa" scope="request"/> Before deleting attributes: data1=${data1}<br> <c:remove var="data1" scope="application" /> After deleting attributes: data1=${data1}<br> </body> </html>
Operation results:
1.4 < c: catch > tag
The <c:catch> tag is used to catch exceptions thrown by content nested in the tag.
<c:catch var="varName" > Code </c:catch>
The var attribute is used to save the captured exception object in the page field.
Give an example:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>c:catch Label</title> </head> <body> <strong>c:catch Label:" </strong><br> <c:catch var="error" > <% int i = 10 / 0; %> </c:catch> Abnormal: ${error} <br> abnormal error.getMessage: ${error.message}<br> abnormal error.getCause: ${error.cause}<br> abnormal error.getStackTrace: ${error.stackTrace}<br> </body> </html>
Operation results:
1.5 < c: if > label
The label < c: if test="/> can construct a simple conditional expression of"if-then"structure.
Property name | Supporting EL | Attribute type | Attribute description |
test | true | Boolean | Conditional expressions for determining whether to process the contents of tags |
var | false | String | The name of the attribute used to specify the execution result of the test attribute |
scope | false | String | Used to specify which web domain the test attribute execution results are saved in |
Give an example:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>c:if Label</title> </head> <body> <strong>c:if Label: </strong><br> <jsp:useBean id="user" scope="session" class="bean.User"> <jsp:setProperty name="user" property="username" value="zhangsan" /> <jsp:setProperty name="user" property="password" value="123" /> </jsp:useBean> <c:if test="${user!=null}"> Welcome: ${user.username} </c:if> <c:if test="${user==null}" > Please login </c:if> </body> </html>
Operation results:
1.6 < c: choose > label
The <c:choose> tag is used to specify combinatorial boundaries for multiple conditional selections. It must be used with <c:when> and <c:otherwise> tags. Use
Three labels < c: choose >, < c: when > and < c: otherwise > can construct a complex conditional judgment structure similar to "if-else if-else".
Give an example:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>c:choose Label</title> </head> <body> <strong>c:choose Label: </strong><br> <c:set var="sex" value="female"/> <c:choose> <c:when test='${sex.equals("male")}'> Gender: male </c:when> <c:when test='${sex.equals("female")}'> Gender: female </c:when> <c:otherwise> Incorrect gender input </c:otherwise> </c:choose> </body> </html>
Operation results:
1.7 < c: for Each > tag
The < c: forEach > tag is used to iterate over elements in a collection object or to iterate over and over the contents of the tag body at a specified number of times.
Property name | Supporting EL | Attribute type | Attribute description |
var | false | String | The attribute name for the element that is currently iterated to is saved in the page field |
items | true | Object | Objects to be iterated |
begin | true | int | If the itmes attribute is specified, the iteration begins with the begin element in the association and ends with the index value of the end. If the items attribute is not specified, the index value of the begin begins with zero and the iteration starts with the value specified by the begin until the end value ends. |
end | true | int | See begin attribute description |
step | true | int | Specify the step size of the iteration |
varStatus | false | String | State information of a collection |
Property name | type | Attribute description |
begin | int | Initial Index of Iteration |
end | int | Iterative End Index |
first | boolean | Is the first element of the current iteration |
last | boolean | Is it the last element of the current iteration? |
count | int | Number of iterations |
current | Object | Current iterated objects |
index | int | Index of current iteration |
step | int | Iterative step size |
Give an example:
<%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="bean.User" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>c:forEach Label</title> </head> <body> <strong>c:forEach Label: </strong><br> <% List<String> list = new ArrayList<>(); list.add("aa"); list.add("bb"); list.add("cc"); list.add("dd"); list.add("ee"); request.setAttribute("list", list); %> <!-- Appoint begin end step attribute --> <c:forEach var="str" begin="0" end="4" step="2" items="${list}" varStatus="varStatus" > ${str}, begin=${varStatus.begin}, end=${varStatus.end}, count=${varStatus.count}, current=${varStatus.current}, first=${varStatus.first},last=${varStatus.last}, index=${varStatus.index}, step=${varStatus.step} <br> </c:forEach> <% List<User> users = new ArrayList<>(); users.add(new User("a", "123")); users.add(new User("b", "123")); users.add(new User("c", "123")); users.add(new User("d", "123")); users.add(new User("e", "123")); request.setAttribute("users", users); %> <!-- No designation begin end step attribute --> <c:forEach var="user" items="${users}" varStatus="varStatus"> ${user}, begin=${varStatus.begin}, end=${varStatus.end}, count=${varStatus.count}, current=${varStatus.current}, first=${varStatus.first},last=${varStatus.last}, index=${varStatus.index}, step=${varStatus.step} <br> </c:forEach> <!-- No designation items --> <c:forEach var="num" begin="1" end="10" > ${num}, </c:forEach> </body> </html>
Operation results: