[] JSTL JSTL tag library of commonly used tags

 

A, JSTL technology

1. JSTL Overview

JSTL (JSP Standard Tag Library), JSP Standard Tag Library, the use of labels may be embedded in the form of complete jsp pages business logic functions. The purpose jstl appear to be mentioned is the same as el script code jsp page. JSTL standard tag library has five sub-standard library, but with the development, often using his core library

 

Tag Library

URI tag library

Prefix

Core

http://java.sun.com/jsp/jstl/core

c

 

2. JSTL download and import

Download JSTL JAR packages from Apache's Web site. Enter "http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/" JSTL Web site to download the installation package. jakarta-taglibs-standard-1.1.2.zip, then install the downloaded package JSTL decompress this case, in lib you can see two JAR files, respectively, and jstl.jar standard.jar.

Wherein, jstl.jar file contains interfaces and classes related JSTL defined in the specification, standard.jar file contains .class files for implementing JSTL and 5 JSTL tag library descriptor file (TLD)

The two jar package into the project lib

 

2. JSTL core library of commonly used tags

1) <c: if test = ""> tag, wherein the test condition is returned boolean

* Introducing tag library: <% @ taglib URI = "http://java.sun.com/jsp/jstl/core" prefix = "C"%>

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <%
12 request.setAttribute("count",11);
13 %>
14 
15     <!-- test代表的返回 -->
16     <c:if test="${count==10}">
17 xxx
18 </c:if>
19     <c:if test="count!=10">
20 yyyy
21 </c:if>
22 
23 
24 </body>
25 </html>

 

usage:

The current user session into a domain, when accessing other resources, can easily session from the field to take the user to see is not landed

(When no login Home login registration, and displays the user name logged off)

 

 

2) <c: forEach> tag

There are two ways

 1.

 

2.

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="domain.*"%>
 4 <%@ page import="java.util.*"%>
 5 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 6 
 7 <!DOCTYPE html>
 8 <html>
 9 <head>
10 <meta charset="UTF-8">
11 <title>Insert title here</title>
12 </head>
13 <body>
14     <%
15         //遍历List<String> strList
16         List<String> strList = new ArrayList<String>();
17         strList.add("itcast");
18         strList.add("itheima");
19         strList.add("ithe");
20         request.setAttribute("strList", strList);
21 
22         //遍历List<User>的值
23         List<User> userList = new ArrayList<User>();
24         User user2 = new User();
25         user2.setId("2");
26         user2.setName("lisi");
27         user2.setPassword("123");
28         userList.add(user2);
29         User user3 = new User();
30         user3.setId("3");
31         user3.setName("wangwu");
32         user3.setPassword("123");
33         userList.add(user3);
34         application.setAttribute("userList", userList);
35 
36         //遍历Map<String,String>的值
37         Map<String, String> strMap = new HashMap<String, String>();
38         strMap.put("name", "lucy");
39         strMap.put("age", "18");
40         strMap.put("addr", "西三");
41         strMap.put("email", "[email protected]");
42         session.setAttribute("strMap", strMap);
43 
44         //遍历Map<String,User>的值
45         Map<String, User> userMap = new HashMap<String, User>();
46         userMap.put("user2", user2);
47         userMap.put("user3", user3);
48         request.setAttribute("userMap", userMap);
49     %>
50 
51     <!-- List<String> strList -->
52     <c:forEach items="${strList }" var="str">
53     ${str }
54     </c:forEach>
55     <br />
56     <br />
57 
58     <!--     List<User> -->
59     <c:forEach items="${userList }" var="user">
60     ${user.name} : ${user.password }
61     <br />
62     </c:forEach>
63     <br />
64     <br />
65 
66     <!-- Map<String,String> -->
67     <c:forEach items="${strMap }" var="entry">
68     ${entry.key } : ${entry.value } 
69     <br />
70     </c:forEach>
71     <br />
72     <br />
73 
74     <!-- Map<String,User> -->
75     <!-- entry.key对应 String;entry.value对应User-->
76     <c:forEach items="${userMap }" var="entry">
77      ${entry.key }: ${entry.value.name }-- ${entry.value.password}
78     </c:forEach>
79 
80 </body>
81 </html>

结果:

 

 

 

Guess you like

Origin www.cnblogs.com/musecho/p/11284181.html