WEB开发的jsp例子标签库(jstl)的使用
<!-- e1 --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% request.setAttribute("attr_request","attr_request值"); session.setAttribute("attr_session","attr_session值"); application.setAttribute("attr_application","attr_application值"); %> <div> <div style="color:red;font-size:30px;">c:out标签的使用</div> <div style="color:red">可以获取request,session,application的值</div> parameter的值(得到不了):<c:out value="${name}"/><br /> <c:out value="${attr_request}"/>---${attr_request}<br /> <c:out value="${attr_session}"/>---${attr_session}<br /> <c:out value="${attr_application}"/>---${attr_application}<br /> </div> <hr /> <% request.setAttribute("same_name","request值"); session.setAttribute("same_name","session值"); application.setAttribute("same_name","application值"); %> <div> <div style="color:red">相同名字的:request优先于session,session优先于application</div> <c:out value="${same_name}"/><br /> </div> <hr /> <% HashMap map = new HashMap(); map.put("name","嵌套值"); request.setAttribute("result",map); ArrayList list = new ArrayList(); HashMap map1 = new HashMap(); map1.put("name","张三"); list.add(map1); HashMap map2 = new HashMap(); map2.put("name","李四"); list.add(map2); request.setAttribute("list",list); %> <div> <div style="color:red">可以嵌套获取值,java也一样</div> <c:out value="${result.name}"/> --- ${result.name}<br /> <c:out value="${list[0].name}"/>--- ${list[0].name}<br /> <c:out value="${list[1].name}"/>---${list[1].name}<br /> </div> </body> </html> <!-- e2 --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <div style="color:red;font-size:30px;">条件判断标签的使用(c:if,c:shoose,c:when,c:otherwise)</div> <hr /> <div> <div style="color:red">c:if的使用</div> <% request.setAttribute("result",true); %> <c:set var="userName" value="张三"/> <c:if test="${result == true}" var="flag"> 欢迎${sessionScope.userName}光临! </c:if> <br /> 你选择的答案:${flag} <hr /> <% HashMap map = new HashMap(); map.put("mark",1); request.setAttribute("result1",map); %> <br /> <c:if test="${result1.mark == 1}"> 还可以通过嵌套使用 </c:if> <hr /> <div style="color:red">c:choose,when,otherwise的使用</div> <% request.setAttribute("month",2); %> <c:choose> <c:when test="${month>0 && month<4}">春</c:when> <c:when test="${month>3 && month<7}">夏</c:when> <c:when test="${month>6 && month<10}">秋</c:when> <c:when test="${month>9 && month<13}">冬</c:when> <c:otherwise>错误</c:otherwise> </c:choose> </div> <hr /> </body> </html> <!-- e3 --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <div style="color:red;font-size:30px;">循环标签的使用(c:forEach,c:fortokens)</div> <hr /> <div> <div style="color:red">c:forEach的使用</div> <% List<String> list = new ArrayList<String>(); list.add("aaa1"); list.add("aaa2"); list.add("aaa3"); request.setAttribute("list",list); %> <c:forEach items="${list}" var="str" varStatus="xh"> ${xh.count}序号从1开始, ${xh.index}序号从0开始, ${xh.last}最后一个序号, ${xh.first}第一个序号, :${str}<br/> </c:forEach> <br /> <hr /> <div style="color:red">c:forEach的使用(空指针不会报错)</div> <% request.setAttribute("list1",null);%> <c:forEach items="${list1}" var="str"> ${str}<br/> </c:forEach> <br /> <hr /> <div style="color:red">c:forEach的使用(数组使用)</div> <% String[] nums = { "1", "2", "3", "4", "5", "6", "7", "8", "9"}; request.setAttribute("nums",nums); %> <c:forEach items="${nums}" var="num"> ${num}<br /> </c:forEach> <br /> <hr /> <div style="color:red">c:forEach的使用(下标从0开始)</div> <c:forEach items="${nums}" var="num" begin="1" end="7" step="2"> ${num}<br /> </c:forEach> <br /> <div style="color:red">c:forEach的使用(数组使用)</div> <% ArrayList array = new ArrayList(); HashMap m = new HashMap(); m.put("name","张三"); array.add(m); m = new HashMap(); m.put("name","李四"); array.add(m); m = new HashMap(); m.put("name","王五"); array.add(m); m = new HashMap(); m.put("name","赵六"); array.add(m); m = new HashMap(); m.put("name","林奇"); array.add(m); request.setAttribute("result_list", array); %> <c:forEach items="${result_list}" var="user"> ${user.name}<br /> </c:forEach> <br /> <hr /> <div style="color:red">c:forTokens的使用</div> <c:forTokens items="a:b:c:d" delims=":" var="token"> <c:out value="${token}"/> </c:forTokens> </body> </html> <!-- e4 --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <div style="color:red;font-size:30px;">格式化标签的使用</div> <hr /> <div style="color:red">fmt:formatNumber的使用</div> <fmt:formatNumber value="0.3" type="number"/><br /> <fmt:formatNumber value="0.3" type="currency"/><br /> <fmt:formatNumber value="0.3" type="percent"/><br /> <br /> <hr /> <div style="color:red">fmt:formatNumber的使用(精确小数点)</div> <fmt:formatNumber value="12.31" pattern=".0000"/><br/> <fmt:formatNumber value="1245678.61" pattern="#"/><br/> <br /> <hr /> <div style="color:red">fmt:formatDate的使用</div> <fmt:formatDate value="<%=new Date()%>" /><br/> <fmt:formatDate value="<%=new Date()%>" type="time"/><br/> <fmt:formatDate value="<%=new Date()%>" pattern="yyyy/MM/dd hh:mm:ss" /><br /> <fmt:formatDate value="<%=new Date()%>" pattern="yyyy-MM-dd HH:mm:ss" /><br /> <fmt:formatDate value="<%=new Date()%>" pattern="yyyy年MM月dd日 hh小时mm分钟ss秒" /><br /> <fmt:formatDate value="<%=new Date()%>" pattern="yy/MM/dd hh:mm:ss" /><br /> <hr> </body> </html> <!-- e5 --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <div style="color:red;font-size:30px;">EL表达式使用</div> <div style="color:red">算术运算符</div> <hr /> 加:1+1 = ${1+1}<br/> 减:1-1 = ${1-1}<br/> 乘:1*2 = ${1*2}<br/> 除: 3/2 = ${3/2}<br/> 模: 3%2 = ${3%2}<br/> <div style="color:red">关系运算符</div> <hr /> 1==1:${1==1}<br/> 1!=1:${1!=1}<br/> 1<2:${1<2}<br/> 1>2:${1>2}<br/> 1<=2:${1<=2}<br/> 1>=2:${1>=2}<br/> <div style="color:red">逻辑运算符</div> <hr /> <% request.setAttribute("a",true); request.setAttribute("b",false); %> a=true,b=false<br/> ${a && b}<br/> ${a || b}<br/> ${!a}<br/> <div style="color:red">三元运算符</div> <hr /> 1 > 1 ? "真" : "假" = ${1 > 1 ? "真" : "假"} <div style="color:red">empty会帮你判断size=0的情况</div> <hr /> <% request.setAttribute("list",new ArrayList());%> <c:if test="${empty list}">empty判断list没有数据</c:if> </body> </html> <!-- e6 --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <div style="color:red;font-size:30px;">set,remove,catch 标签使用</div> <div style="color:red">set的使用</div> <hr /> <c:set var="userName" value="张三" scope="request" /> <c:set var="userName" value="李四" scope="session" /> <c:set var="userName" value="王五" scope="application" /> <%=request.getAttribute("userName") %><br/> <%=session.getAttribute("userName") %><br/> <%=application.getAttribute("userName") %><br/> <br/> <div style="color:red">remove的使用</div> <hr /> <c:remove var="userName" scope="request" /> <c:remove var="userName" scope="session" /> <c:remove var="userName" scope="application" /> remove后的值:<%=request.getAttribute("userName") %><br/> remove后的值:<%=session.getAttribute("userName") %><br/> remove后的值:<%=application.getAttribute("userName") %><br/> <br/> <div style="color:red">catch的使用</div> <hr /> <c:catch var="error_Message"> <% int i = Integer.parseInt("a"); %> </c:catch> ${error_Message} </body> </html>
要使用jstl标签库的话:需要自己下载jar包 jstl.jar&standard.jar
效果图: