Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

3.7. JSTL(JavaServer Pages Standard Tag Library)

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

3.7.1. c:set

设置变量

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

or

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


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

3.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" />
			
			

3.7.3. c:url

生成URL

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

3.7.4. c:remove

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

3.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" />
			
			

3.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>	
			
			

3.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>			
			
			

3.7.8. c:forEach

3.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>
				
				

3.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>
				
				

3.7.9. JSTL fmt Tag setBundle Example

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>			
			
			

3.7.9.1. Language Package

				
<fmt:setLocale value="en" />				
				
				

3.7.9.2. fmt:message 变量

				
<fmt:message key="js" bundle="${config}" var="val" />
<c:out value="${val}"/>				
				
				
comments powered by Disqus