[JavaWeb] JSTL tag library

Keywords: PHP Java JSP Oracle Apache

JSTL tag library

JSTL Standard Label Library;

JSTL is used to simplify JSP development and improve code readability and maintainability.

JSTL is defined by SUN(Oracle) and implemented by Apache Tomcat team.

Reference to JSTL Core Library

  • Core is the most important tag library of JSTL, which provides the basic functions of JSTL.
  • <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  • JSTL Core Label Library is defined by META-INF/c.tld in taglibs-standard-impl.jar

Conditional judgement

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    <%
        int x = 15;
        request.setAttribute("x", x);
    %>
    <c:if test="${ requestScope.x > 0 && requestScope.x <= 10 }">
        <div style="color:blue;font-weight:bold;">1-10 Integers between</div>
    </c:if>
    <c:if test="${ requestScope.x > 10 && requestScope.x <= 20 }">
        <div style="color:red;font-weight:bold;">11-20 Integers between</div>
    </c:if>
</body>
</html>

Multiple Conditional Judgment

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    <c:choose>
        <c:when test="${ param.day == 'MONDAY' }">
            <h2 style="color:blue;">Monday</h2>
        </c:when>
        <c:when test="${ param.day == 'TUESDAY' }">
            <h2 style="color:blue;">Tuesday</h2>
        </c:when>
        <c:when test="${ param.day == 'WEDNESDAY' }">
            <h2 style="color:blue;">Wednesday</h2>
        </c:when>
        <c:when test="${ param.day == 'THURSDAY' }">
            <h2 style="color:blue;">Thursday</h2>
        </c:when>
        <c:when test="${ param.day == 'FRIDAY' }">
            <h2 style="color:blue;">Friday</h2>
        </c:when>
        <c:when test="${ param.day == 'SATURDAY' }">
            <h2 style="color:blue;">Saturday</h2>
        </c:when>
        <c:when test="${ param.day == 'SUNDAY' }">
            <h2 style="color:blue;">Sunday</h2>
        </c:when>
        <c:otherwise>
            <h2 style="color:red;">The content is not right!</h2>
        </c:otherwise>
    </c:choose>
</body>
</html>

Ergodic cycle

MonthServlet.java

package jstl;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class MonthServlet
 */
@WebServlet("/month")
public class MonthServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MonthServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Set<String> setMonths = new HashSet();
        setMonths.add("January");
        setMonths.add("February");
        setMonths.add("March");
        setMonths.add("April");
        setMonths.add("May");
        request.setAttribute("months", setMonths);
        request.getRequestDispatcher("/month.jsp").forward(request, response);
    }
 
}

month.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>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <c:forEach items="${requestScope.months }" var="c" varStatus="idx">
        <h2>${idx.index + 1} - ${c }</h2>
    </c:forEach>
</body>
</html>

c:out

  • Belong to Core
  • <c:out value="${nothing}" default="none"> </c:out> (nothing=null) converts the original null value to "none"
  • <c:out value="${html}" escapeXml= "true"> </c:out> Browser will display HTML source code without explanation.

fmt formatted tag library

  • FMT formatted tag library URI:http://java.sun.com/jsp/jstl/fmt
  • <fmt: formatDate value="" pattern="> formatted date label
  • <fmt: formatNumber value="" pattern="> formatted numeric label

formatDate pattern

yyyy Four years
MM Two months
dd Two days
HH 24 hour system
hh 12 hour system
mm Minute
ss Seconds
SSS Millisecond

formatNumber pattern

  • "0.00": Keep two decimal places
  • "0,000.00": Separated by three-one, with two decimal digits reserved

Posted by farel on Sat, 12 Oct 2019 12:10:36 -0700