<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
设置变量
<c:set var="foo" scope="request" value="helloworld">
or
<%request.setAttribute("foo","helloworld") %>
<c:out value="${requestScope.foo}"/>
输出变量variable的内容
<c:out value="${variable}"/>
<%=request.getParameter("UA")%>
相当于
<c:out value="${param.UA}"/>
默认值
<c:out value="${param.UA}" default="UA-69658002-1" />
<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" />
<%@ 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>
<%@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>
<%@ 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>
<%@ 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>
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>