jstl之核心标签

JSP 标准标签库(JSTL)
  JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。
  JSTL支持通用的、结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签。 除了这些,它还提供了一个框架来使用集成JSTL的自定义标签。
  根据JSTL标签所提供的功能,可以将其分为5个类别。
    核心标签,格式化标签,SQL 标签,XML 标签,JSTL 函数

jstl安装配置:
  从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip),解压后jstl.jar,standard.jar把加入到配置环境中

核心标签
  核心标签是最常用的JSTL标签。引用核心标签库的语法如下:
  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

举例测试:
UserServlet.java

package com.blueocean.jstl.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        String name = "jstl-Name";
        request.setAttribute("name", name);
        
        List<String> stringList = new ArrayList<String>();
        stringList.add("String001");
        stringList.add("String002");
        stringList.add("String003");
        stringList.add("String004");
        stringList.add("String005");
        request.setAttribute("stringList", stringList);
        
        Map<String, String> stringMap = new HashMap<String, String>();
        stringMap.put("k1", "v1");
        stringMap.put("k2", "v2");
        stringMap.put("k3", "v3");
        stringMap.put("k4", "v4");
        stringMap.put("k5", "v5");
        request.setAttribute("stringMap", stringMap);
        
        
        request.getRequestDispatcher("/jstl.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        
    }

    
}

jstl.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib prefix="c" 
           uri="http://java.sun.com/jstl/core_rt" %>
<!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=ISO-8859-1">
<title>jstl测试</title>
</head>
<body>
<div>
    <div>
        <div>EL测试</div>
        <div>
            ${name }
        </div>
        <div><c:out value="${name }"></c:out></div>
    </div>
    <div>
        <div>测试c:set c:remove c:out</div>
        <div>测试c:set--<c:set var="cName" value="jstlcName" /></div>
        <div>测试c:out--<c:out value="${cName }" /></div>
        <div>测试c:remove--<c:remove var="cName"/></div>
        <div>测试c:out--<c:out value="${cName }" /></div>
    </div>
    <hr>
    <div>
        <div>测试List取值,c:forEach,c:if,c:out</div>
        <div>
            <c:forEach var="str" items="${stringList }">
                <c:if test="${str != null}" var="res">
                    <div>str:<c:out value="${str }" />:<c:out value="${res }" /></div>
                </c:if>
            </c:forEach>
        </div>
    </div>
    <hr>
    <div>
        <div>测试c:choose,c:when,c:otherwires</div>
        <div>
            <c:set var="num" value="100"/>
            <c:choose>
                <c:when test="${num >= 10 }" >大于等于10</c:when>
                <c:otherwise>小于10</c:otherwise>
            </c:choose>
        </div>
    </div>
    <hr>
    <div>
        <div>测试Map取值</div>
        <div>
            <c:forEach var="str" items="${stringMap }">
                <c:out value="${str.key }"></c:out> : 
                <c:out value="${str.value }"></c:out><br>
            </c:forEach>
        </div>
    </div>
    <hr>
</div>
</body>
</html>

运行结果

原文地址:https://www.cnblogs.com/djoker/p/6393942.html