Hướng Dẫn Sử Dụng JSP Standard Tag Library (JSTL) Trong Java
Có thể bạn quan tâm
Tạo project và cấu hình các file cần sử dụng:
Tạo project demojstl gồm các file như sau:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>demojstl</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.itphutran.controller"/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>package com.itphutran.controller
File SinhVien.java
package com.itphutran.entities; public class SinhVien { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public SinhVien(int id, String name) { super(); this.id = id; this.name = name; } public SinhVien() { super(); } }SinhVienController.java
package com.itphutran.controller; import java.util.ArrayList; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import com.itphutran.entities.SinhVien; @Controller @RequestMapping("/sinh-vien") public class SinhVienController { @GetMapping("") public String index(ModelMap modelMap){ ArrayList<SinhVien> listSV = new ArrayList<SinhVien>(); listSV.add(new SinhVien(1,"Đoàn Phương Linh")); listSV.add(new SinhVien(2, "Lưu Bị")); listSV.add(new SinhVien(3, "Tào Tháo")); listSV.add(new SinhVien(4, "Phú IT")); modelMap.addAttribute("listSV",listSV); return "index"; } }index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//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>Danh sách sinh viên</title> </head> <body style="margin:0 auto; width:1024px"> <h1>Danh sách sinh viên</h1> <table width="100%" border="1" cellpadding="1" cellspacing="1"> <tr> <th width="10%">Mã SV</th> <th width="50%">Tên SV</th> </tr> <tr> <td>1</td> <td>Nguyễn Văn A</td> </tr> </table> </body> </html>Để hỗ trợ cho việc demo, các bạn cần nắm kỹ các toán tử của biểu thưc EL
| Bằng với (equals) | == | eq |
| Không bằng (Not equals) | != | ne |
| Nhỏ hơn (Less than) | < | lt |
| Lớn hơn (Greater than) | > | gt |
| Nhỏ hơn hoặc bằng (Less than or equals) | <= | le |
| Lớn hơn hoặc bằng (Greater than or equals) | >= | ge |
Cấu trúc dự án:

Sử dụng các thẻ JSTL – JSP Standard Tag Library:
- Sử dụng thẻ <c:forEach> -dùng để lặp quét hết danh sách nào đó:
Tại trang index.jsp để hiển thị danh sách từ controler truyền sang view, chúng ta áp dụng vòng lặp foreach của thẻ tiêu chuẩn jstl như sau:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//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>Danh sách sinh viên</title> </head> <body style="margin:0 auto; width:1024px"> <h1>Danh sách sinh viên</h1> <table width="100%" border="1" cellpadding="1" cellspacing="1"> <tr> <th width="10%">Mã SV</th> <th width="50%">Tên SV</th> </tr> <c:forEach items="${listSV }" var="objSV"> <tr> <td>${objSV.id }</td> <td>${objSV.name }</td> </tr> </c:forEach> </table> </body> </html>Để bạn đọc dễ hình dung, chúng tôi có mô hình bên dưới:

Kết quả:

- Sử dụng <c:if> được sử dụng để kiểm tra một điều kiện nào đó là đúng hay sai:
Chúng ta sẽ làm demo ví dụ bên dưới:
Bài toán: Kiểm tra xem id của sinh viên thứ 3 trong danh sách là số chẵn hay lẻ, nếu là lẻ thì in ra mã của sinh viên đó là số lẻ.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//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>Danh sách sinh viên</title> </head> <body style="margin:0 auto; width:1024px"> <h1>Danh sách sinh viên</h1> <table width="100%" border="1" cellpadding="1" cellspacing="1"> <tr> <th width="10%">Mã SV</th> <th width="50%">Tên SV</th> </tr> <c:forEach items="${listSV }" var="objSV"> <tr> <td>${objSV.id }</td> <td>${objSV.name }</td> </tr> </c:forEach> </table> <h2>IF - JSTL </h2> <c:if test="${listSV.get(2).id % 2 != 0}"> <p>Sinh viên ${listSV.get(2).name } có ID là ${listSV.get(2).id} số lẻ!</p> </c:if> </body> </html>Bạn đọc có thể hình dùng và dễ hiểu hơn qua mô hình bên dưới:

Lưu ý: <c:if> chỉ có duy nhất một trường hợp, không có else.
Kết quả:

- c:choose – c:when – c:otherwise: Tương tự như trường hợp sử dụng if -> else if -> else if… else hoặc như trường hợp switch case (có thể hiểu như vậy với trường hợp c:choose – c:when – c:otherwise trong JSTL )
Cũng với bài toán trên, Bài toán: Kiểm tra xem id của sinh viên thứ 3 trong danh sách là số chẵn hay lẻ, nếu là lẻ thì in ra mã của sinh viên đó là số lẻ ngược lại in ra số chẵn.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//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>Danh sách sinh viên</title> </head> <body style="margin:0 auto; width:1024px"> <h1>Danh sách sinh viên</h1> <table width="100%" border="1" cellpadding="1" cellspacing="1"> <tr> <th width="10%">Mã SV</th> <th width="50%">Tên SV</th> </tr> <c:forEach items="${listSV }" var="objSV"> <tr> <td>${objSV.id }</td> <td>${objSV.name }</td> </tr> </c:forEach> </table> <h2>IF - JSTL </h2> <c:if test="${listSV.get(2).id % 2 != 0}"> <p>Sinh viên ${listSV.get(2).name } có ID là ${listSV.get(2).id} số lẻ!</p> </c:if> <h2>c:choose - c:when - c:otherwise </h2> <c:choose> <c:when test="${listSV.get(2).id % 2 != 0}"> <p>Sinh viên ${listSV.get(2).name } có ID là ${listSV.get(2).id} số lẻ!</p> </c:when> <c:otherwise> <p>Sinh viên ${listSV.get(2).name } có ID là ${listSV.get(2).id} số Chẵn!</p> </c:otherwise> </c:choose> </body> </html>
Lưu ý: Nếu có thêm một trường hợp bất kỳ nào nữa chỉ cần thêm thẻ cwhen.
Kết quả:

- Thẻ <c:out> hiển thị các kết quả của một biểu thức, tương tự như cách làm việc của sciptlet <% =%>, khác biệt là <c:out> có thể sử dụng “.” để truy cập vào các thuộc tính của đối tượng. <c:out> có thể tự động escape nội dung XML.
escape có ý nghĩa là nếu có các ký tự đặc biệt < > hoặc & nằm ở trong <c:out> khi in ra nó sẽ được chuyển thành < và > và &
- Chẳng hạn:
- <c:out value = “${‘<atag> &’}” />
- In ra (Xem trong source của trang trên trình duyệt).
- <atag< &
Kết quả trên trình duyệt:

Nhưng khi các bạn view source (F12) thì thấy rằng những ký tự đặc biệt như < > được mã hóa:

-
c:set: dùng để khai báo một biến trong jsp dùng thư viện thẻ chuẩn JSTL (SP Standard Tag Library (JSTL) trong Java)
Lấy một ví dụ đơn giản: ở đây bài toán đặt ra là khai báo một biến là fullname, sau đó in ra.
Như vậy tôi sẽ khai báo như sau:
file index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//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>Danh sách sinh viên</title> </head> <body style="margin:0 auto; width:1024px"> <h1>Danh sách sinh viên</h1> <table width="100%" border="1" cellpadding="1" cellspacing="1"> <tr> <th width="10%">Mã SV</th> <th width="50%">Tên SV</th> </tr> <c:forEach items="${listSV }" var="objSV"> <tr> <td>${objSV.id }</td> <td>${objSV.name }</td> </tr> </c:forEach> </table> <h2>IF - JSTL </h2> <c:if test="${listSV.get(2).id % 2 != 0}"> <p>Sinh viên ${listSV.get(2).name } có ID là ${listSV.get(2).id} số lẻ!</p> </c:if> <h2>c:choose - c:when - c:otherwise </h2> <c:choose> <c:when test="${listSV.get(2).id % 2 != 0}"> <p>Sinh viên ${listSV.get(2).name } có ID là ${listSV.get(2).id} số lẻ!</p> </c:when> <c:otherwise> <p>Sinh viên ${listSV.get(2).name } có ID là ${listSV.get(2).id} số Chẵn!</p> </c:otherwise> </c:choose> <c:out value="${'<aa> ok '}"></c:out> <h2> c:set - JSTL </h2> <c:set var="fullname" value="Phú IT"></c:set> <c:out value="${fullname }"></c:out> </body> </html>Bạn đọc có thể xem sơ đồ sau để dễ dàng hiểu:

Kết quả hiển thị trên trình duyệt:

Trên là những thẻ JSTL thường xuyên sử dụng khi các bạn lập trình web với jsp/servlet hay các framework như spring, struts….vv. Những thẻ còn lại đơn giản, cũng như ít sử dụng cho nên tôi không nhắc đến ở đây.
Từ khóa » Thư Viện Trong Jsp
-
Hướng Dẫn Sử Dụng Java JSP Standard Tag Library (JSTL)
-
Thư Viện Thẻ Chuẩn - Standard Tag Library (JSTL) Trong JSP
-
Java JSP Standard Tag Library (JSTL) Tutorial With Examples
-
Download Thư Viện Jstl Trong Jsp Archives
-
Sự Khác Nhau Giữa Jsp Và Jstl Là Gì ? Nếu Khác Người ... - In4tintuc
-
Jsp Standard Tag Library - Tài Liệu Text - 123doc
-
Custom Tag Library – Tạo Ra Tag Library Trong JSP - Kieu Trong Khanh
-
[Update] Hướng Dẫn Sử Dụng Java JSP Standard Tag Library (JSTL)
-
JSTL Functions (TLDDoc Generated Documentation)
-
Lê Lục Bình - HelpEx
-
JavaServer Pages Standard Tag Library (JSTL) - KungFu Tech
-
Sự Khác Nhau Giữa Jsp Và Jstl Là Gì ? Nếu Khác Người Khác ...
-
[Lập Trình Java Web] - Bài 15: JSP Standard Tags Library (JSTL ...
-
[Spring Framework] JSTL - Code Lean