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" %>


	
		
		Insert title here
	
	
		
	

二.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标签用于声明变量,声明后在后续的获取中就可以通过该声明来获取变量了,使用示例如下:


	
		
		Insert title here
	
	
		
		${marks }
	

jstl常用标签和函数_第1张图片

2.remove标签

remove标签用于移除声明的变量,一般与set标签成对使用,以下是使用示例:


	
		
		Insert title here
	
	
		
		
		s${marks }s
	

jstl常用标签和函数_第2张图片

3.if标签

if标签与if语句效果一样用来控制分支,其中test中写判断条件,使用示例如下:


	
		
		Insert title here
	
	
		
			不相等
		
	

jstl常用标签和函数_第3张图片

4.choose标签

choose标签的作用就类似于java中的switch语句,它在使用时需要有内嵌的when和otherwise子标签的,就像switch语句中要有case语句一样,以下是使用示例:


	
		
		Insert title here
	
	
		
			
				不相等
			
			
				相等
			
		
	

jstl常用标签和函数_第4张图片

5.forEach标签

该标签和java中的foreach循环方式一样,使用示例如下:


	
		
		Insert title here
	
	
		
			${score }
			

jstl常用标签和函数_第5张图片

三.JSTL函数库

由于jstl函数库是基于java中的String类的方法写成的,所以功能基本一致,这里就以两个常用的函数为例。

1.length()

该函数返回对象的长度,使用示例如下:


	
		
		Insert title here
	
	
		${fn:length(scores) }
	

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);
	}
}

以下是截取的代码: 


	
		
		Insert title here
	
	
		${fn:substring(scores,0,7) }
	

jstl常用标签和函数_第6张图片

你可能感兴趣的:(jstl常用标签和函数)