目录
一.JSTL
JSTL(JSP Standard Tag Library,JSP标准标签库)是一个开源代码的JSP标签库
1.在使用jstl之前需要引入两个jar包:standard-1.1.2.jar和jstl.jar
2.引完jar包需要在代码前面引入标签库和函数库,写法如下:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!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>
</body>
</html>
二.JSTL标签库
首先创建一个整型数组放入request作用域中:
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int []scores = {100,0,60,88};
request.setAttribute("scores", scores);
request.getRequestDispatcher("target.jsp").forward(request, response);
}
}
1.set标签
set标签用于声明变量,声明后在后续的获取中就可以通过该声明来获取变量了,使用示例如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set var="marks" value="${scores[0] }"></c:set>
${marks }
</body>
</html>
2.remove标签
remove标签用于移除声明的变量,一般与set标签成对使用,以下是使用示例:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set var="marks" value="${scores[0] }"></c:set>
<c:remove var="marks"/>
s${marks }s
</body>
</html>
3.if标签
if标签与if语句效果一样用来控制分支,其中test中写判断条件,使用示例如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${scores[0]!=scores[1] }">
不相等
</c:if>
</body>
</html>
4.choose标签
choose标签的作用就类似于java中的switch语句,它在使用时需要有内嵌的when和otherwise子标签的,就像switch语句中要有case语句一样,以下是使用示例:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:choose>
<c:when test="${scores[0]!=scores[1] }">
不相等
</c:when>
<c:otherwise>
相等
</c:otherwise>
</c:choose>
</body>
</html>
5.forEach标签
该标签和java中的foreach循环方式一样,使用示例如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach var="score" items="${scores }">
${score }
<br/>
</c:forEach>
</body>
</html>
三.JSTL函数库
由于jstl函数库是基于java中的String类的方法写成的,所以功能基本一致,这里就以两个常用的函数为例。
1.length()
该函数返回对象的长度,使用示例如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${fn:length(scores) }
</body>
</html>
2.substring()
该函数用于截取指定位置的内容,使用示例如下:
先将一个字符串传入request中:
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("scores", "Tom is a dog");
request.getRequestDispatcher("target.jsp").forward(request, response);
}
}
以下是截取的代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${fn:substring(scores,0,7) }
</body>
</html>