Jslfl【软件开发技术笔记】

jstl常用基础

格式化数字日期
Html代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>  
 
<fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd"/>  
 
<fmt:formatNumber pattern=".00" value="11"></fmt:formatNumber>  
11.00  
<fmt:formatNumber pattern="0.00" value="0"></fmt:formatNumber>  
0.00  
<fmt:formatNumber pattern="0,000.00" value="2233333333"></fmt:formatNumber>  
2,233,333,333.00  
<fmt:formatNumber pattern="#.00" value="2233333333"></fmt:formatNumber>  
2233333333.00  
<fmt:formatNumber pattern="#.#" value="2233333333"></fmt:formatNumber>  
2233333333

方法和其它

1
2
3
4
5
6
7
8
9
10
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>  
 
<select id="typeid" name="typeid">  
    <c:forEach var="rt" items="${list_rt}">  
     <option value="${rt.codeid}" <c:if test="${rt.codeid eq resources.typeid}"> selected="selected"</c:if>>  
         ${rt.codename}  
     </option>  
   </c:forEach>  
</select>

内置字符串处理方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
函数名 函数说明 使用举例
contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}">
containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:containsIgnoreCase(name, searchString)}">
endsWith 判断字符串是否以另外字符串结束 <c:if test="${fn:endsWith(filename, ".txt")}">
escapeXml 把一些字符转成XML表示,例如<字符应该转为&lt; ${fn:escapeXml(param:info)}
indexOf 子字符串在母字符串中出现的位置 ${fn:indexOf(name, "-")}
join 将数组中的数据联合成一个新字符串,并使用指定字符格开 ${fn:join(array, ";")}
length 获取字符串的长度,或者数组的大小 ${fn:length(shoppingCart.products)}
replace 替换字符串中指定的字符 ${fn:replace(text, "-", "&#8226;")}
split 把字符串按照指定字符切分 ${fn:split(customerNames, ";")}
startsWith 判断字符串是否以某个子串开始 <c:if test="${fn:startsWith(product.id, "100-")}">
substring 获取子串 ${fn:substring(zip, 6, -1)}
substringAfter 获取从某个字符所在位置开始的子串
${fn:substringAfter(zip, "-")}
substringBefore 获取从开始到某个字符所在位置的子串 ${fn:substringBefore(zip, "-")}
toLowerCase 转为小写 ${fn.toLowerCase(product.name)}
toUpperCase 转为大写字符 ${fn.UpperCase(product.name)}
trim 去除字符串前后的空格 ${fn.trim(name)}


得到页面请求参数:
${param.myname}


if-else:
<c:choose>
   <c:when test="test1">
     结果1
   </c:when>
   <c:when test="test2">
     结果2
   </c:when>
   <c:otherwise>
    否则
   </c:otherwise>
</c:choose>

如果test1=true,则执行结果1,如果test2=true,则执行结果2,如果前面条件都为false,则执行otherwise

JSTL与EL表达式(为空判断)

一、循环遍历集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
1、在jsp中引入标准函数声明
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  

2、若要判断集合的大小,则需要引入如下声明
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

3、如何使用jstl判断集合是否为空
${user}为集合,user为集合名
<c:if test="${empty user}">无信息!</c:if>为空
<c:if test="${!empty user}">其它</c:if>非空

4、如何取得集合的大小
${fn:length(集合名<如果是session中的集合则应该加上sessionScope.键>)}
${fn:length(map)}

5、如何取得保存在session中的对象?
${sessionScope.键.对象属性}

6、varStatus显示循环变量的状态
例:<tag:forEach var="currentFood" items="${sessionScope.foods}" varStatus="status"
<tag:if test="${status%2==1}">
.....................
</tag:if>
</tag:forEach>
其中status这个变量就记录了循环变量的状态

7、如何遍历Map集合
<c:forEach var="foodmap" items="${sessionScope.cart}" varStatus="status">
<c:set var="subtotal" value="${foodmap.value.bean.foodPrice*foodmap.value.quantity}"></c:set>
</c:forEach>
遍历Map集合的值:
foodmap:保存session中的map
foodmap.value:取得map的值,即获取保存在map中的一个对象
要获取对象中的数据,必须用foodmap.value.quantity去点对象的属性(quantity就是对象的属性)
8、对象属性为空显示默认值
<c:forEach var="customer" items="${customers}">
    <tr>
      <td><c:out value="${customer.lastName}"/></td>
      <td><c:out value="${customer.phoneHome}" default="no home phone specified"/></td>
      <td>
        <c:out value="${customer.phoneCell}" escapeXml="false">
          <font color="red">no cell phone specified</font>
        </c:out>
      </td>
    </tr>
  </c:forEach>

, , , , ,

Comments are currently closed.