---
title: "JSP中JSTL 标签总结"
author: "小色帝"
date: "2016-07-25T14:23:13Z"
tags: []
categories: ["Web前端"]
slug: "jsp-jstl"
draft: false
lastmod: "2016-07-25T14:23:13Z"
id: 2313268
url: "https://wangdingxin.iteye.com/blog/2313268"
---

很久没碰jsp页面相关的开发了，一直是后台service即使最近使用的也是freemarker 
springMVC后，不用struts2标签后页面的输出都要改用比较原始的标签了 
今天一天的问题记录，后期不定时补充以扩充自己的问题集 

jsp头部引入使用的标签 
<%@ taglib prefix="c" uri="/WEB-INF/tld/c.tld"%> 
<%@ taglib prefix="fmt" uri="/WEB-INF/tld/fmt.tld"%> 
<%@ taglib prefix="spring" uri="/WEB-INF/tld/spring.tld"%> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>


①循环 
controller中 new ModelMap().addAttribute("list",list); 将list数据放到request中后需要在页面中使用的时候需要循环 
<c:forEach items="${list}" var="m"> 
name:${m.name} 
</c:forEach> 

②判断 
jsp页面中使用if判断语句，以循环为例，需要list的length属性 
PS：forEach可以循环包括List Set 等常用集合 
场景：多个结果使用竖线分隔，最后一个不需要竖线（内地|港澳|欧美） 
<c:forEach items="${list}" var="s" varStatus="st">${s.name} 
<c:if test="${fn:length(list)!=(st.index+1)}">|</c:if> 
</c:forEach> 
varStatus="st" 可以用来代表元素的序号从0开始，如list有5条数据st最大可到4 
if判断条件在页面中要使用${}对获取值进行处理后将结果放入test的判断中，即test引号内结果可认为是true，false 
对于list的length使用 fn:length(list) 获取，list中没有length或者size属性，需要java处理后才能得知list个数或者长度。 
st中包含index属性，即表示循环的值0~4，因为length从1开始，对比的时候需要将index+1后进行判断 
依旧假设list元素有5个，在list循环到最后一次时 st.index 的值为4，判断结果为相等时跳出循环，即不显示最后一个竖线（|） 

③标签和java程序嵌套使用 
场景：当list中存在外键实体如 list的一个元素m，m.type.category.name(一首歌曲有多个关联关系 type属性为：轻音乐，柔情歌曲。category属性同为mp3)此时如果需要对最大的分类进行循环显示--mp3，就会造成显示2次mp3（xx之歌||） 
大多数人会建议在controller中进行处理，不过在实体不会进行改变的条件下可以使用标签和java程序嵌套方式处理显示 
<c:forEach items="${list}" var="m"> 
<% 
Film f = (Film)pageContext.getAttribute("m"); 
Set<FilmTypeMap> mtp = mv.getFilmTypeMap();  //这里需要Film实体中含FilmTypeMap这个实体Set集合 
Map<Long,String> map = new HashMap<Long,String>(); 
for(FilmTypeMap tm:mtp){ 
map.put(tm.geFilmType().getFilmCategory().getId(), tm.getFilmType().getFilmCategory().getName()); 
} 
for(String str:map.values()){ 
out.print(" "); 
} 
%> 

④日期处理 
按照 yyyy-MM-dd 方式显示 
<fmt:formatDate value="${time}" pattern="yyyy-MM-dd"/> 


20120808 一天遇到的问题整理记录 

⑤有c:if 没有c:else的解决方法 
类似choose case的条件判断 
<c:choose> 
<c:when test="">if结果</c:when> 
<c:otherwise>else结果</c:otherwise> 
</c:choose> 

⑥在jsp页面中取列表长度 如list的size 或数组长度 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<c:out value="${fn:length(list)}"></c:out> 




fn标签 
<%@tablib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
${fn:substring("你要截取的字符串"),beginIndex,endIndex} 

下面是JSTL中自带的方法列表以及其描述 

函数名 函数说明 使用举例  
fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}">  
fn:containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:containsIgnoreCase(name, searchString)}">  
fn:endsWith 判断字符串是否以另外字符串结束 <c:if test="${fn:endsWith(filename, ".txt")}">  
fn:escapeXml 把一些字符转成XML表示，例如<字符应该转为&lt; ${fn:escapeXml(param:info)}  
fn:indexOf 子字符串在母字符串中出现的位置 ${fn:indexOf(name, "-")}  
fn:join 将数组中的数据联合成一个新字符串，并使用指定字符格开 ${fn:join(array, ";")}  
fn:length 获取字符串的长度，或者数组的大小 ${fn:length(shoppingCart.products)}  
fn:replace 替换字符串中指定的字符 ${fn:replace(text, "-", "&#149;")}  
fn:split 把字符串按照指定字符切分 ${fn:split(customerNames, ";")}  
fn:startsWith 判断字符串是否以某个子串开始 <c:if test="${fn:startsWith(product.id, "100-")}">  
fn:substring 获取子串 ${fn:substring(zip, 6, -1)}  
fn:substringAfter 获取从某个字符所在位置开始的子串 
${fn:substringAfter(zip, "-")}  
fn:substringBefore 获取从开始到某个字符所在位置的子串 ${fn:substringBefore(zip, "-")}  
fn:toLowerCase 转为小写 ${fn.toLowerCase(product.name)}  
fn:toUpperCase 转为大写字符 ${fn.UpperCase(product.name)}  
fn:trim 去除字符串前后的空格 ${fn.trim(name)}