• 【JavaWeb】JSTL标签库


    JSTL标签库

    JSTL标准标签库;

    JSTL用于简化JSP开发,提高代码的可读性与可维护性;

    JSTL由SUN(Oracle)定义规范,由Apache Tomcat团队实现;

    引用JSTL核心库

    • 核心标签库(Core)是JSTL最重要的标签库,提供了JSTL的基础功能
    • <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    • JSTL核心标签库在taglibs-standard-impl.jar由META-INF/c.tld定义

    条件判断

    <%@ 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>
    

    多重条件判断

    <%@ 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>
    

    遍历循环

    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

    • 属于核心标签库(Core)
    • <c:out value="${nothing}" default="无"></c:out>(nothing=null)将原本为null的值转义为"无"
    • <c:out value="${html}" escapeXml="true"></c:out>浏览器不进行解释,将html源代码显示

    fmt格式化标签库

    • fmt格式化标签库URI:http://java.sun.com/jsp/jstl/fmt
    • <fmt:formatDate value="" pattern="">格式化日期标签
    • <fmt:formatNumber value="" pattern="">格式化数字标签

    formatDate pattern

    | yyyy | 四位年 | | ---- | -------- | | MM | 两位月 | | dd | 两位日 | | HH | 24小时制 | | hh | 12小时制 | | mm | 分钟 | | ss | 秒数 | | SSS | 毫秒 |

    formatNumber pattern

    • "0.00":保留两位小数
    • "0,000.00":三位一分隔,保留两位小数
  • 相关阅读:
    智能指针
    C++学习之对类中的成员函数的定义和声明最后添加一个const作用
    动态链接,静态链接库
    Java 位运算
    Java 工具类
    Java 枚举
    Java 内部类
    Java 异常机制
    Java hashCode 和 equals
    Java 字节流和字符流
  • 原文地址:https://www.cnblogs.com/huowuyan/p/11286232.html
Copyright © 2020-2023  润新知