Author: java_wxid

JSTL Tag Library JSTL Tag Library is the JSP Standard Tag Library. Is a constantly improving open source JSP tag library. EL expressions are primarily intended to replace expression scripts in JSPS, while tag libraries are intended to replace code scripts. This makes the entire JSP page more concise.

JSTL consists of five tag libraries with different functions. Function Scope URI prefix

Core tag library - key formatting http://java.sun.com/jsp/jstl/fmt http://java.sun.com/jsp/jstl/core c FMT function http://java.sun.com/jsp/jstl/functions fn (not use) http://java.sun.com/jsp/jstl/sql database SQL XML (not use) http://java.sun.com/jsp/jstl/xml xCopy the code

CORE tag library <% @taglib prefix= “c” uri= “java.sun.com/jsp/jstl/co… %> XML tag library <% @taglib prefix= “x” URI = “java.sun.com/jsp/jstl/xm… %> FMT tag library <% @taglib prefix= “FMT” URI = “java.sun.com/jsp/jstl/fm…” SQL tag library <% @taglib prefix= “SQL” URI = “java.sun.com/jsp/jstl/sq…” %> FUNCTIONS tag library <% @taglib prefix= “fn” uri= “java.sun.com/jsp/jstl/fu…” % >

Procedure Step 1 Import the JSTL tag library jar package taglibs-standard-imp-1.2.1. jar taglibs-standard-spec-1.2.1.jar

<% @taglib uri= “java.sun.com/jsp/jstl/co…” The prefix = “c” % >

The core library uses
to store data in the domain and to modify the properties of the bean object in the domain or the value of a key in the Map collection

The < body > < % - i. < c: set / > -- % > < % - the domain object. SetAttriubte (key, value); The scope which the domain object attribute set use page, request, session, and application of var is the key to save the value attribute set the value of you want to save - % > <! ${requestScope. Key1}<br/> <c:set scope="request" var="key1" value="value1"></c:set> ${ requestScope.key1 }<br/> <hr/> <% Map<String,Object> map = new HashMap<String,Object>(); Map. put("aa", "before modification "); map.put("bb", "bbValue"); request.setAttribute("map", map); SetXxxx (new value); setXxxx(new value); Put (key, new value); The target property indicates which bean object or map set you want to modify, or the key value of the map set you want to modify --%> Before modifying: ${requestScope. Map}<br/> <c:set target="${requestScope. Map}" property="aa" value=" after "></c:set> ${ requestScope.map }<br/> </body>Copy the code

The
if tag can be used for judgment purposes.

<% > <c:if test="${12 == 12}"> <h1>12 = 12 blah blah blah blah... </h1> </c:if>Copy the code

The < C :choose> < C :when> < C :otherwise> tag is basically the same as switch, case, and default.

<% request.setAttribute("height", 136); % > < % - choose. The when. Two points need to be noted in otherwise: Choose --%> <c:choose><%-- switch --%> <%-- when Case --%> <c:when test="${requestScope. Height >= 190}"> </h1> <c:when test="${requestScope. Height >= 180}"> </h1> <c:when test="${requestScope. Height >= 170}"> </h1> <c:when test="${requestScope. Height >= 160}"> </h1> </c:when> <c:otherwise><%-- default --%> <c:choose> <c:when test="${ requestScope.height >= 150 }"> <h1>150</h1> </c:when> <c:when test="${ requestScope.height >= 140 }"> <h1>140</h1> </c:when> <c:when test="${ requestScope.height >=  130 }"> <h1>130</h1> </c:when> </c:choose> </c:otherwise> </c:choose>Copy the code

1, it can generate a URL address, directly output 2, can generate the URL address to a domain

<! Context is the path of the project you are in. The default is the current project --> <c: URL value="/ f.sp "context="/ ABC ">< c:param name="username" value="wzg168"></c:param> <c:param name="password" value="123456"></c:param> </c:url> <br/> <! SetAttribute (key, address); setAttribute(key, address); Session. SetAttribute (url address); --> <c:url value="/f.jsp" context="/abc" scope="session" var="url"> <c:param name="username" value="wzg168"></c:param> <c:param name="password" value="123456"></c:param> </c:url> ${sessionscope.url}Copy the code

forEach tags are traversed

List — List contains the Person class with attributes such as id, username, password, age, and phone number 4. Iterate over the Map collection

Test code:

<%@page import="java.util.HashMap"%> <%@page import="java.util.Map"%> <%@page import="com.atguigu.pojo.Student"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <! PUBLIC DOCTYPE HTML "- / / / / W3C DTD HTML 4.01 Transitional / / EN" "http://www.w3.org/TR/html4/loose.dtd" > < HTML > < head > < meta  http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style type="text/css"> table{ width: 500px; border: 1px solid red; border-collapse: collapse; } th , td{ border: 1px solid red; } </style> </head> <body> <! -- 1. Walk through 1 to 10, Var is the data being traversed. -> <c:forEach BEGIN ="1" end="10" var=" I "> ${I} </c:forEach <hr/> <! - 2. Traverse Object array -- > < % request. The setAttribute (" arr ", new String [] {" aaaa ", "BBBB", "CCCC}"); % > <! <c:forEach items="${requestScope.arr}" var="item"> ${item}<br/> </c:forEach> <hr/> <! <% List<Student> List = new ArrayList<Student>(); --> <% List<Student> List = new ArrayList<Student>(); for (int i = 0; i < 10; i++) { list.add(new Student(i,"name"+i,"pass"+i,18+i,"phone"+i)); } request.setAttribute("list", list); % > < table > < tr > < th > id < / th > < th > user name < / th > < th > password < / th > < th > age < / th > < th > call < / th > < th > action < / th > < / tr > <! -- step sets the step size, how the index moves after each iteration, <c:forEach begin="1" end="7" items="${requestScope.list}" step="1" var="stu" varStatus="status"> <c:if test="${ status.count == 3 }"> <tr style="background-color: green;" > </c:if> <c:if test="${ status.count ! = 3 }"> <tr> </c:if> <td>${ stu.id }</td> <td>${ stu.username }</td> <td>${ stu.password }</td> <td>${ stu.age }</td> <td>${ stu.phone }</td> <td>${ status.step }</td> </tr> </c:forEach> </table> <hr/> <! <% Map<String,Object> Map = new HashMap<String,Object>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); request.setAttribute("map", map); %> <c:forEach items="${ requestScope.map }" var="entry"> ${ entry.value }<br/> </c:forEach> </body> </html>Copy the code