jstl常用标签和函数

目录

 

一.JSTL

二.JSTL标签库

1.set标签

2.remove标签

3.if标签

4.choose标签

5.forEach标签

三.JSTL函数库

1.length()

2.substring()


一.JSTL

JSTL(JSP Standard Tag Library,JSP标准标签库)是一个开源代码的JSP标签库

1.在使用jstl之前需要引入两个jar包:standard-1.1.2.jarjstl.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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值