JSTL使用
Posted 曹军
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSTL使用相关的知识,希望对你有一定的参考价值。
1.标签函数库
核心标签库 c
I18N格式标签库 fmt
SQL标签库 sql
XML标签库 xml
函数标签库 fn
2.JSTL支持EL
二:表达式标签
1.介绍
2.c:out
可以对敏感的字符进行自动转换,强于EL表达式。
语法:
<c:out value="${EL表达式}" >
属性:
3.c:set
为变量或者javaBean属性赋值的工作。
属性:
4.c:remove
删除存于scope范围内的变量。
属性:
5.程序customer.java
1 package bean; 2 3 public class Customer { 4 private String name; 5 private int age; 6 public Customer() { 7 8 } 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public int getAge() { 16 return age; 17 } 18 public void setAge(int age) { 19 this.age = age; 20 } 21 public Customer(String name, int age) { 22 super(); 23 this.name = name; 24 this.age = age; 25 } 26 27 }
6.index.jsp
1 <%@page import="bean.Customer"%> 2 <%@ page language="java" contentType="text/html; charset=utf-8" 3 pageEncoding="utf-8"%> 4 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <% 13 Customer customer=new Customer(); 14 customer.setAge(100); 15 request.setAttribute("customer", customer); 16 %> 17 <!-- c:set 为变量赋值的方式--> 18 <c:set var="book" value="<java1>" scope="request"></c:set> 19 <c:set var="date" value="1999-9-1" scope="session"></c:set> 20 <!-- c:set --> 21 <c:set target="${requestScope.customer}" property="age" value="88"></c:set> 22 23 <!-- c:out 为javabean属性赋值的方式--> 24 <c:out value="${requestScope.book}"></c:out> 25 <br> 26 <c:out value="${requestScope.customer.age}"></c:out> 27 <br> 28 <c:out value="${sessionScope.date}"></c:out> 29 30 <!-- c:remove --> 31 <c:remove var="date" scope="session"/> 32 <br> 33 <c:out value="${sessionScope.date}"></c:out> 34 </body> 35 </html>
7.效果
三:流程控制标签
1.c:if
属性:
2.程序
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <!-- c:if --> 12 <c:set var="id" value="96" scope="request"></c:set> 13 <c:if test="${requestScope.id>18}">成年人</c:if> 14 <br> 15 16 <!-- c:if的好处:可以将判断结果保存起来,以备以后使用下面是用法 --> 17 <c:if test="${requestScope.id>18}" var="isAdult" scope="request">成年人</c:if> 18 <br> 19 <c:out value="${requestScope.isAdult}"></c:out> 20 <br> 21 22 <!-- c:choose c:when c:otherwise --> 23 <c:set var="age" value="11116" scope="session"></c:set> 24 <c:choose > 25 <c:when test="${session.age > 60}"> 26 老年 27 </c:when> 28 <c:otherwise> 29 未成年。。。。。。。。。。。 30 </c:otherwise> 31 </c:choose> 32 33 </body> 34 </html>
四:迭代标签
1.c:forEach
为循环控制,可以将Collection中的成员循环浏览一遍。
属性:
varStatus的用法:
可以获取索引
当前第几个
是否是第一个
是否是最后一个
2.程序
1 <%@page import="java.util.HashMap"%> 2 <%@page import="java.util.Map"%> 3 <%@page import="java.util.List"%> 4 <%@page import="java.util.ArrayList"%> 5 <%@page import="bean.Customer"%> 6 <%@ page language="java" contentType="text/html; charset=utf-8" 7 pageEncoding="utf-8"%> 8 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 10 <html> 11 <head> 12 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 13 <title>Insert title here</title> 14 </head> 15 <body> 16 <!-- c:foreach 的用法 遍历Collection--> 17 <% 18 List<Customer> customer=new ArrayList<Customer>(); 19 customer.add(new Customer("A",1)); 20 customer.add(new Customer("B",2)); 21 customer.add(new Customer("C",3)); 22 customer.add(new Customer("D",4)); 23 customer.add(new Customer("E",5)); 24 request.setAttribute("customer", customer); 25 %> 26 <c:forEach items="${requestScope.customer}" begin="0" step="2" end="4" var="cust"> 27 ${cust.name} : ${cust.age}<br> 28 </c:forEach> 29 30 <br> 31 <!-- c:foreach 的用法 遍历数组--> 32 <% 33 String[] strs=new String[]{"A","B","C"}; 34 request.setAttribute("strs", strs); 35 %> 36 <c:forEach items="${requestScope.strs}" var="str"> 37 ${str} 38 </c:forEach> 39 40 <br><br> 41 <!-- c:foreach中的status属性的用法 --> 42 <c:forEach items="${requestScope.customer}" var="cust" varStatus="status"> 43 ${status.index}:${status.count}:${status.first}:${status.last}:${cust.name}<br> 44 </c:forEach> 45 46 <br> 47 <!-- c:foreach中的用法,遍历map --> 48 <% 49 Map<String,Customer> customer2=new HashMap<String,Customer>(); 50 customer2.put("a",new Customer("A",1)); 51 customer2.put("b",new Customer("B",2)); 52 customer2.put("c",new Customer("C",3)); 53 customer2.put("d",new Customer("D",4)); 54 customer2.put("e",new Customer("E",5)); 55 request.setAttribute("customer2", customer2); 56 %> 57 <c:forEach items="${requestScope.customer2}" var="customer2"> 58 ${customer2.key }:${customer2.value.name}:${customer2.value.age}<br> 59 </c:forEach> 60 61 </body> 62 </html>
3.效果
4.c:forToken
处理字符串的,类似与String的split方法。
5.程序
1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2 pageEncoding="ISO-8859-1jsp&el&jstl-01