[] JavaWeb JSTL tag libraries

JSTL tag libraries

JSTL standard tag library;

JSTL JSP to simplify development, improve the readability and maintainability of the code;

JSTL, implemented by SUN (Oracle) specification defined by the Apache Tomcat team;

JSTL core library reference

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

Conditional

<%@ 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之间的整数</div>
    </c:if>
    <c:if test="${ requestScope.x > 10 && requestScope.x <= 20 }">
        <div style="color:red;font-weight:bold;">11-20之间的整数</div>
    </c:if>
</body>
</html>

Multiple conditional

<%@ 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;">星期一</h2>
        </c:when>
        <c:when test="${ param.day == 'TUESDAY' }">
            <h2 style="color:blue;">星期二</h2>
        </c:when>
        <c:when test="${ param.day == 'WEDNESDAY' }">
            <h2 style="color:blue;">星期三</h2>
        </c:when>
        <c:when test="${ param.day == 'THURSDAY' }">
            <h2 style="color:blue;">星期四</h2>
        </c:when>
        <c:when test="${ param.day == 'FRIDAY' }">
            <h2 style="color:blue;">星期五</h2>
        </c:when>
        <c:when test="${ param.day == 'SATURDAY' }">
            <h2 style="color:blue;">星期六</h2>
        </c:when>
        <c:when test="${ param.day == 'SUNDAY' }">
            <h2 style="color:blue;">星期日</h2>
        </c:when>
        <c:otherwise>
            <h2 style="color:red;">内容不对哦!</h2>
        </c:otherwise>
    </c:choose>
</body>
</html>

Traversal 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 the core tag library (Core)
  • <c:out value="${nothing}" default="无"></c:out>(Nothing = null) originally escaped as "no" null value
  • <c:out value="${html}" escapeXml="true"></c:out>Browser does not interpret the html source code display

fmt formatting tag library

  • fmt formatting tag library URI:http://java.sun.com/jsp/jstl/fmt
  • <fmt:formatDate value="" pattern="">Date Format Label
  • <fmt:formatNumber value="" pattern="">Format digital label

formatDate pattern

yyyy Four years
MM Two months
dd Two days
HH 24-hour clock
hh 12-hour
mm minute
ss The number of seconds
SSS millisecond

formatNumber pattern

  • "0.00": two decimal places
  • "0,000.00": a three separate, two decimal places

Guess you like

Origin www.cnblogs.com/huowuyan/p/11286232.html