JSP 標準標籤庫(JSTL)之最經常使用的JSTL標籤總結

 

JSP標準標籤庫(JSTL)是一個JSP標籤集合,它封裝了JSP應用的通用核心功能。html

Apache Tomcat安裝JSTL 庫步驟以下:java

  • 從Apache的標準標籤庫中下載的二進包(jakarta-taglibs-standard-current.zip)。下載地址:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
  • 下 載jakarta-taglibs-standard-1.1.1.zip 包並解壓,將jakarta-taglibs-standard-1.1.1/lib/下的兩個jar文件:standard.jar和jstl.jar 文件拷貝到/WEB-INF/lib/下。

使用任何庫,你必須在每一個JSP文件中的頭部包含<taglib>標籤。git

這裏只介紹倆種經常使用的標籤。1. 核心標籤,2. 時間格式化標籤。apache

 

1、核心標籤。jsp

核心標籤是最經常使用的JSTL標籤。引用核心標籤庫的語法以下:編碼

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

(1)<c:if>spa

     <c:if>標籤判斷表達式的值,若是表達式的值爲真則執行其主體內容。code

屬性orm

描述htm

是否必要

默認值

test

條件

var

用於存儲條件結果的變量

scope

var屬性的做用域

page

 

判斷是否爲空或者是否不爲空:

<c:if  test="${empty info.paymentName || info.paymentName eq ''}">
----
</c:if> <c:if test="${not empty info.paymentName && info.paymentName ne ''}"> 
${info.paymentName}
</c:if>

 

判斷的用法:

<c:if test="${info.status eq 0}"> 支付失敗 </c:if> <c:if test="${info.status eq 1}"> 已支付 </c:if>

 

forEash 的應用:

<c:forEach items="${result.newRepaymentLoanList}" var="info">

<span >${info.loanQuota }</span>
<span ><a href="<%=basePath%>project/info/${info.loanId }">查看</a>
</c:forEach>

 

<c:forEach items="${orderList}" var="info" varStatus="status">

</c:forEach>

 

 

 

2、JSTL格式化標籤用來格式化並輸出文本、日期、時間、數字。引用格式化標籤庫的語法以下:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

 

 

<fmt:formatNumber>標籤有以下屬性:

屬性 描述 是否必要 默認值
value 要顯示的數字
type NUMBER,CURRENCY,或 PERCENT類型 Number
pattern 指定一個自定義的格式化模式用與輸出
currencyCode 貨幣碼(當type="currency"時) 取決於默認區域
currencySymbol 貨幣符號 (當 type="currency"時) 取決於默認區域
groupingUsed 是否對數字分組 (TRUE 或 FALSE) true
maxIntegerDigits 整型數最大的位數
minIntegerDigits 整型數最小的位數
maxFractionDigits 小數點後最大的位數
minFractionDigits 小數點後最小的位數
var 存儲格式化數字的變量 Print to page
scope var屬性的做用域 page

若是type屬性爲percent或number,那麼您就能夠使用其它幾個格式化數字屬性。

maxIntegerDigits屬性和 minIntegerDigits屬性容許您指定整數的長度。

若實際數字超過了maxIntegerDigits所指定的最大值,則數字將會被截斷。

有一些屬性容許您指定小數點後的位數。minFractionalDigits屬性和maxFractionalDigits屬性容許您指定小數點後的位數。

若實際的數字超出了所指定的範圍,則這個數字會被截斷。

數字分組能夠用來在每三個數字中插入一個逗號。groupingIsUsed屬性用來指定是否使用數字分組。當與minIntegerDigits屬性一同使用時,就必需要很當心地來獲取預期的結果了。

您或許會使用pattern屬性。這個屬性能夠讓您在對數字編碼時包含指定的字符。接下來的表格中列出了這些字符。

符號 描述
0 表明一位數字
E 使用指數格式
# 表明一位數字,若沒有則顯示0
. 小數點
, 數字分組分隔符
; 分隔格式
- 使用默認負數前綴
% 百分數
? 千分數
¤ 貨幣符號,使用實際的貨幣符號代替
X 指定能夠做爲前綴或後綴的字符
' 在前綴或後綴中引用特殊字符

 

 

 (1)時間格式化。

 

<fmt:formatDate value="${record.loginTime }" type="both" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate>

 

<fmt:formatDate value="${info.lastModified }" type="date" pattern="yyyy-MM-dd HH:mm:ss"/>

 

 

 (2)金額格式化。

 

 新建一個NumberTest.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<html>
<head>
<title>JSTL fmt:formatNumber Tag</title>
</head>
<body>
    <h3>Number Format:</h3>
    <!-- 設置 balance的值爲 120000.2309 -->
    <c:set var="balance" value="120000.2309" />
    <p>
        Formatted Number (1):
        <fmt:formatNumber value="${balance}" type="currency" />
    </p>
    <p>
        Formatted Number (2):
        <fmt:formatNumber type="number" maxIntegerDigits="3" value="${balance}" />
    </p>
    <p>
        Formatted Number (3):
        <fmt:formatNumber type="number" maxFractionDigits="3" value="${balance}" />
    </p>
    <p>
        Formatted Number (4):
        <fmt:formatNumber type="number" groupingUsed="false" value="${balance}" />
    </p>
    <p>
        Formatted Number (5):
        <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" />
    </p>
    <p>
        Formatted Number (6):
        <fmt:formatNumber type="percent" minFractionDigits="10" value="${balance}" />
    </p>
    <p>
        Formatted Number (7):
        <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" />
    </p>
    <p>
        Formatted Number (8):
        <fmt:formatNumber type="number" pattern="###.###E0" value="${balance}" />
    </p>
    <p>
        Currency in USA :
        <fmt:setLocale value="en_US" />
        <fmt:formatNumber value="${balance}" type="currency" />
    </p>
</body>
</html>

 

<fmt:formatNumber pattern="#,##0.00" type="number" value="${loanQuota}"></fmt:formatNumber>

 

<fmt:formatNumber pattern="#,##0.00" type="number" value="${loanQuota}"/>

 

 

 效果以下:

Number Format:

Formatted Number (1): ¥120,000.23

Formatted Number (2): 000.231

Formatted Number (3): 120,000.231

Formatted Number (4): 120000.231

Formatted Number (5): 023%

Formatted Number (6): 12,000,023.0900000000%

Formatted Number (7): 023%

Formatted Number (8): 120E3

Currency in USA : $120,000.23 

 

 

 

嵌入菜單文件的方式等等:

 

<%@include file="../meta.jsp"%>

 

<jsp:include page="../top.jsp">
     <jsp:param name="menuShow" value="1" />
     <jsp:param name="showIndex" value="3" />
</jsp:include>

 

<jsp:include page="left.jsp"></jsp:include>

 

 

讀取 controller 綁定過來的值:

 

${result.past3RepayCount }
相關文章
相關標籤/搜索