Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

100.7. JSTL(JavaServer Pages Standard Tag Library)

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

100.7.1. c:set

设置变量

		
<c:set var="foo" scope="request" value="helloworld">

or

<%request.setAttribute("foo","helloworld") %>


<c:out value="${requestScope.foo}"/>
		
		

100.7.1.1. c:remove

			
<c:remove var="message" scope="session" />
			
			

100.7.2. c:out

输出变量variable的内容

		
<c:out value="${variable}"/>


<%=request.getParameter("UA")%>
相当于
<c:out value="${param.UA}"/>

默认值
<c:out value="${param.UA}" default="UA-69658002-1" />
		
		

100.7.3. c:url

生成URL

		
<c:url value="/news/china/"/>
		
		

100.7.4. c:redirect

		
<c:redirect url="/index.html"/>
<c:redirect url="http://www.netkiller.cn"/>		
		
		

100.7.5. c:import

		
<c:import url="http://www.netkiller.cn" />		

<c:import var="html" url="http://www.netkiller.cn"/>
<c:out value="${html}"/>	
		
		

传递GET参数

		
<c:import url="http://www.netkiller.cn" > 
	<c:param name="id" value="10" />
	<c:param name="name" value="neo" /> 
</c:import> 			
		
		

异常处理

		
<c:catch var="exception">

  <c:import url="ftp://ftp.example.com/package/README"/>

</c:catch>

<c:if test="${not empty exception}">

  Sorry, the remote content is not currently available.

</c:if>			
		
		

在Context间切换

		
server.conf: 
<Context  reloadable="true" crossContext="true" /> 

<c:import url="/path/to/file.jsp" context=/project1" /> 			
<c:import url="/path/to/file.jsp" context=/project2" />
		
		

100.7.6. c:if

		
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
   <p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>	
		
		

100.7.6.1. boolean

			
<c:if test="${theBooleanVariable}">It's true!</c:if>
<c:if test="${not theBooleanVariable}">It's false!</c:if>
			
			

100.7.7. c:choose

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

<c:choose>
	<c:when test="${session.auth eq 'true' }">
		
	</c:when>
	<c:otherwise>
		
	</c:otherwise>
</c:choose>			
		
		

实现 if else/else if / else

		
<c:choose>
   <c:when test="${..}">...</c:when> <!-- if condition -->
   <c:when test="${..}">...</c:when> <!-- else if condition -->
   <c:otherwise>...</c:otherwise>    <!-- else condition -->
</c:choose>			
		
		

100.7.8. c:forEach

100.7.8.1. List 处理

			
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	${bookList}
	<br>

	<c:forEach items="${bookList}" var="node">
		<c:out value="${node}"></c:out><br>
	</c:forEach>

</body>
</html>
			
			

100.7.8.2. Map 处理

			
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<c:forEach items="${channel}" var="node">
		<a href="<c:out value="${node.value}"></c:out>"><c:out value="${node.key}"></c:out></a>
        <br/>
	</c:forEach>
</body>
</html>
			
			

100.7.9. empty 判断是否为空

			
	<c:if test="${empty session.member }">

	</c:if>
			
			

100.7.10. JSTL fmt Tag setBundle Example

100.7.10.1. fmt:message

src/resources/config.properties

			
Name=Neo
Address=Shenzhen
Number=13322993040
			
			
			
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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>JSTL fmt:setBundle Tag</title>
</head>
<body>
	<fmt:setBundle basename="resources.config" var="config" />
	<fmt:message key="Name" bundle="${config}" />

	<fmt:message key="Address" bundle="${config}" />

	<fmt:message key="Number" bundle="${config}" />
</body>
</html>
			
			
			
	<fmt:bundle basename="lang">
		<fmt:message key="Name" />
		<fmt:message key="Address" />
	</fmt:bundle>			
			
			
Language Package
				
<fmt:setLocale value="en" />				
				
				
fmt:message 变量
				
<fmt:message key="js" bundle="${config}" var="val" />
<c:out value="${val}"/>				
					
					

100.7.10.2. 

			
<fmt:setTimeZone value="Europe/London" scope="session"/>			
			
			
			
<fmt:formatDate value="${isoDate}" type="both"/>
2004-5-31 23:59:59

<fmt:formatDate value="${date}" type="date"/>
2004-4-1

<fmt:formatDate value="${isoDate}" type="time"/>
23:59:59

<fmt:formatDate value="${isoDate}" type="date" dateStyle="default"/>
2004-5-31

<fmt:formatDate value="${isoDate}" type="date" dateStyle="short"/>
04-5-31

<fmt:formatDate value="${isoDate}" type="date" dateStyle="medium"/>
2004-5-31

<fmt:formatDate value="${isoDate}" type="date" dateStyle="long"/>
2004年5月31日

<fmt:formatDate value="${isoDate}" type="date" dateStyle="full"/>
2004年5月31日 星期一

<fmt:formatDate value="${isoDate}" type="time" timeStyle="default"/>
23:59:59

<fmt:formatDate value="${isoDate}" type="time" timeStyle="short"/>
下午11:59

<fmt:formatDate value="${isoDate}" type="time" timeStyle="medium"/>
23:59:59

<fmt:formatDate value="${isoDate}" type="time" timeStyle="long"/>
下午11时59分59秒

<fmt:formatDate value="${isoDate}" type="time" timeStyle="full"/>
下午11时59分59秒 CDT

<fmt:formatDate value="${date}" type="both" pattern="EEEE, MMMM d, yyyy HH:mm:ss Z"/>
星期四, 四月 1, 2004 13:30:00 -0600

<fmt:formatDate value="${isoDate}" type="both" pattern="d MMM yy, h:m:s a zzzz/>
31 五月 04, 11:59:59 下午 中央夏令时 

格式模式:
  d   月中的某一天。一位数的日期没有前导零。    
  dd   月中的某一天。一位数的日期有一个前导零。    
  ddd   周中某天的缩写名称,在   AbbreviatedDayNames   中定义。    
  dddd   周中某天的完整名称,在   DayNames   中定义。    
  M   月份数字。一位数的月份没有前导零。    
  MM   月份数字。一位数的月份有一个前导零。    
  MMM   月份的缩写名称,在   AbbreviatedMonthNames   中定义。    
  MMMM   月份的完整名称,在   MonthNames   中定义。    
  y   不包含纪元的年份。如果不包含纪元的年份小于   10,则显示不具有前导零的年份。    
  yy   不包含纪元的年份。如果不包含纪元的年份小于   10,则显示具有前导零的年份。    
  yyyy   包括纪元的四位数的年份。    
  gg   时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。    
  h   12   小时制的小时。一位数的小时数没有前导零。    
  hh   12   小时制的小时。一位数的小时数有前导零。    
  H   24   小时制的小时。一位数的小时数没有前导零。    
  HH   24   小时制的小时。一位数的小时数有前导零。     
  m   分钟。一位数的分钟数没有前导零。    
  mm   分钟。一位数的分钟数有一个前导零。    
  s   秒。一位数的秒数没有前导零。    
  ss   秒。一位数的秒数有一个前导零。

<fmt:formatDate value="${xx}" pattern="dd/MM/yyyy HH:mm aa"/>和

<fmt:formatDate value="${xx}" pattern="dd/MM/yyyy hh:mm aa"/>  对于0点显示的结果不一样


<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm:ss z" timeZone="GMT"/>

<fmt:formatDate value="${dateValue}" pattern="yyyy-MM-dd HH:mm:ss z" timeZone="US/Eastern"/>