jsp-TagLib标签库

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
    <title>TagLib标签库</title>
</head>
<body>
    <%--引用jar包--%>
    <%--默认情况下,Servlet 2.3 / JSP 1.2是不支持EL表达式的,而Servlet 2.4 / JSP 2.0支持。注意修改web.xml--%>

    <h1>JSTL核心标签</h1>
    <%--<c:out>标签用来显示一个表达式的结果,与<%= %>作用相似,它们的区别就是<c:out>标签可以直接通过"."操作符来访问属性。
    举例来说,如果想要访问order.orderId,只需要这样写:<c:out value="order.orderId">--%>
    <h3><c:out> 实例</h3>
    <c:out value="&lt要显示的数据对象(未使用转义字符)&gt" escapeXml="true" default="默认值"></c:out><br/>
    <c:out value="&lt要显示的数据对象(使用转义字符)&gt" escapeXml="false" default="默认值"></c:out><br/>
    <c:out value="${null}" escapeXml="false">使用的表达式结果为null,则输出该默认值</c:out><br/>

    <h3><c:set> 实例</h3>
    <c:set var="salary0" scope="session" value="${2000*2}"/>
    <c:out value="${salary0}"/>

    <h3><c:catch> 实例</h3>
    <c:catch var ="catchException">
        <% int x = 5/0;%>
    </c:catch>
    <c:if test = "${catchException != null}">
        <p>异常为 : ${catchException} <br />
            发生了异常: ${catchException.message}</p>
    </c:if>


    <h3><c:if> 实例</h3>
    <c:set var="salary" scope="session" value="${2000*2}"/>
    <c:if test="${salary > 2000}">
        <p>我的工资为: <c:out value="${salary}"/><p>
    </c:if>

    <h3><c:choose> 实例</h3>
    <%--<c:set var="salary" scope="session" value="${2000*2}"/>--%>
    <p>你的工资为 : <c:out value="${salary}"/></p>
    <c:choose>
        <c:when test="${salary <= 0}">
            太惨了。
        </c:when>
        <c:when test="${salary > 1000}">
            不错的薪水,还能生活。
        </c:when>
        <c:otherwise>
            什么都没有。
        </c:otherwise>
    </c:choose>


    <h3><c:forEach> 实例</h3>
    <c:forEach var="i" begin="1" end="5">
        Item <c:out value="${i}"/><p>
    </c:forEach>


    <h3><c:param> 和<c:url> 实例</h3>
    <c:url var="myURL" value="jsp/demo3.jsp">
        <c:param name="name" value="zhangsan"/>
        <c:param name="url" value="www.jingzhengu.com"/>
    </c:url>
    <a href="/<c:out value="${myURL}"/>">
    使用 <c:param> 为指定URL发送两个参数。</a>



   <%-- <h3><c:redirect> 实例</h3>
    <c:redirect url="http://www.jingzhengu.com"/>--%>



    <h1>JSTL格式化标签</h1>
    <h3>数字格式化:</h3>
    <c:set var="balance" scope="session" value="120000.2309" />
    <p>格式化数字 (1): <fmt:formatNumber value="${balance}" type="currency"/></p>
    <p>格式化数字 (2): <fmt:formatNumber type="number" maxIntegerDigits="3" value="${balance}" /></p>
    <p>格式化数字 (3): <fmt:formatNumber type="number" maxFractionDigits="3" value="${balance}" /></p>
    <p>格式化数字 (4): <fmt:formatNumber type="number" groupingUsed="false" value="${balance}" /></p>
    <p>格式化数字 (5): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /></p>
    <p>格式化数字 (6): <fmt:formatNumber type="percent" minFractionDigits="10" value="${balance}" /></p>
    <p>格式化数字 (7): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /></p>
    <p>格式化数字 (8): <fmt:formatNumber type="number" pattern="###.###E0" value="${balance}" /></p>
    <p>美元 :<fmt:setLocale value="en_US"/>
        <fmt:formatNumber value="${balance}" type="currency"/></p>

    <h3>数字解析:</h3>
    <fmt:parseNumber var="i" type="number" value="${balance}" />
    <p>数字解析 (1) : <c:out value="${i}" /></p>
    <fmt:parseNumber var="i" integerOnly="true"
                     type="number" value="${balance}" />
    <p>数字解析 (2) : <c:out value="${i}" /></p>



    <h3>日期格式化:</h3>
    <c:set var="now" value="<%=new java.util.Date()%>" />

    <p>日期格式化 (1): <fmt:formatDate type="time"
                                  value="${now}" /></p>
    <p>日期格式化 (2): <fmt:formatDate type="date"
                                  value="${now}" /></p>
    <p>日期格式化 (3): <fmt:formatDate type="both"
                                  value="${now}" /></p>
    <p>日期格式化 (4): <fmt:formatDate type="both"
                                  dateStyle="short" timeStyle="short"
                                  value="${now}" /></p>
    <p>日期格式化 (5): <fmt:formatDate type="both"
                                  dateStyle="medium" timeStyle="medium"
                                  value="${now}" /></p>
    <p>日期格式化 (6): <fmt:formatDate type="both"
                                  dateStyle="long" timeStyle="long"
                                  value="${now}" /></p>
    <p>日期格式化 (7): <fmt:formatDate pattern="yyyy-MM-dd"
                                  value="${now}" /></p>

    <h3>日期解析:</h3>
    <c:set var="now1" value="20-10-2015" />

    <fmt:parseDate value="${now1}" var="parsedEmpDate"
                   pattern="dd-MM-yyyy" />
    <p>解析后的日期为: <c:out value="${parsedEmpDate}" /></p>



    <h1>JSTL函数</h1>

    <h3><c:substring> 实例</h3>
    <c:set var="string1" value="This is first String."/>
    <c:set var="string2" value="${fn:substring(string1, 5, 15)}" />
    <p>生成的子字符串为 : ${string2}</p>


    <h3><c:contains> 实例</h3>
    <c:set var="theString" value="I am from runoob"/>
    <c:if test="${fn:contains(theString, 'runoob')}">
    <p>找到 runoob<p>
    </c:if>
    <c:if test="${fn:contains(theString, 'RUNOOB')}">
    <p>找到 RUNOOB<p>
    </c:if>

</body>
</html>

  

原文地址:https://www.cnblogs.com/lijiasnong/p/8336581.html