jstl 使用

1、需要导入:

   1)jstl.jar

   2)standard.jar

引入:jsp相应的核心库:<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

其中preifix表示是什么标签。 url表示引用什么标签。会报错:org.apache.jasper.JasperException: /action/demo7.jsp (line: 15, column: 0) According to TLD or attribute directive in tag file, attribute test does not accept any expressions

需要更改为:http://java.sun.com/jstl/core_rt 即可解决。

格式:<c:if  test="el表达式(${xx})"> </c:if>

没有else判断,如果需要重新在写一个。

但是需要注意:引用http://java.sun.com/jstl/core

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
 3 <html>
 4 <head>
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <c:if test="${2>4}">2>4</c:if>
 9 <c:if test="${4>2}">4>2</c:if>
10 </body>
11 </html>

 2、forEach标签:

格式:<c:foreach items="el表达式(${xx}})var 输出变量 varstatus:输出状态值比如:count 输出多少个值 current:当前输出值>${x} </c:foreach>

 1 <%@ page import="java.util.*" %>
 2 
 3 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 4 <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
 5 <html>
 6 <head>
 7     <title>Title</title>
 8 </head>
 9 <body>
10 <%
11     List<String> list=new LinkedList<>();
12     list.add("ok");
13     list.add("oop");
14     request.setAttribute("list",list);
15     Map<String,String> map=new HashMap<>();
16     map.put("oop","java");
17     map.put("func","python");
18     request.setAttribute("map",map);
19 %>
20 <%--格式:<c:foreach  items="el表达式(${xx}})var 输出变量 varstatus:输出状态值比如:count 输出多少个值 current:当前输出值>
21 ${x} </c:foreach>}"--%>
22 <c:forEach items="${list}" var="n" varStatus="status">${n} ${status.count}<br></c:forEach>
23 <c:forEach  items="${map}" var="k" >
24     ${k.value} ${k.key}
25 </c:forEach>
26 </body>
27 </html>

 3:c:set  c:choose   c:when c:otherwise

c:set:相当于pagecontext.setAttriute()如果scope不设置默认是当前page域

c:choose c:when c:otherwise:是一对,类似if else

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
 3 <html>
 4 <head>
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <c:set var="lan" value="2" scope="request"/><!--相当于pagecontext.setAttriute()如果scope不设置默认是当前page域-->
 9 <!-- c:choose  c:when  c:otherwise是一对,类似if else   -->-->
10 <c:choose>
11     <c:when test="${lan==2}">ok</c:when>
12     <c:otherwise>其他</c:otherwise>
13 </c:choose>
14 
15 
16 </body>
17 </html>

 4;函数库使用。

导入:taglibs-standard-impl-1.2.5.jar  相当于(相当于之前的jstl.jar,属于接口定义类)

  taglibs-standard-spec-1.2.5.jar (相当于之前的standard.jar,属于实现类)

之前的jstl.jar和standar.jar已经合并到tomcat下,并更名:taglib。下载地址:http://tomcat.apache.org/download-taglibs.cgi

http://www.bubuko.com/infodetail-1077023.html  该问有详细介绍。

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
 3 <html>
 4 <head>
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 ${fn:toUpperCase("oop")}
 9 ${fn}
10 </body>
11 </html>

其中需要注意 fn不是标签,是函数。不要写 <c:fn,其他方法如下。

原文地址:https://www.cnblogs.com/evilliu/p/8626748.html