JSTL标签库

    科技2025-06-13  31

    1.什么是JSTL?

        JSTL的英文全称是JavaServer Pages Standard Tag Library,中文全称是JSP标准标签库,是一个不断完善的开放源代码的JSP标签库。

        EL表达式主要是为了替代jsp中的表达式脚本,而JSTL标签库是为了代替代码脚本。这样会使得整个jsp页面更加简洁。

    2. JSTL标签库有哪些?

    <%@  taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>         CORE(核心)标签库<%@  taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/xml" %>           XML标签库<%@  taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/fmt" %>            FMT(格式化)标签库<%@  taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/sql" %>            SQL标签库<%@  taglib  prefix="c"  uri="http://java.sun.com/jsp/jstl/functions" %>   FUNCTIONS标签库

    3.如何使用JSTL?

    1.首先,先导入JSTL的jar包。

    2.使用taglib指令引入标签库。

    4.常用的核心标签:

    1.set

    <hr> <%-- set可以给域对象设置参数: scope设置给哪个域对象设置参数。 var表示key。 value表示key对应的值。 --%> <c:set scope="request" var="abc" value="abcValue"/> 保存之后:${requestScope.abc} <br> <hr>

    结果:

     2.if

    <%-- if标签用来做if判断,这里不能用else test属性表示判断的条件(使用EL表达式输出) --%> <c:if test="${12 == 12}"> <h1>12等于12</h1> </c:if> <c:if test="${12 != 12}"> <h1>12不等于12</h1> </c:if>

    结果:

     3.choose

    <%-- <c:choose> 相当于switch <c:when> 相当于case <c:otherwise> 相当于default 这里不用手动break 作用和switch ... case ... default非常接近 注意: 1.choose标签里不能使用html注释,要使用jsp注释 2.when标签的父标签一定要是choose标签(嵌套的时候要注意) --%> <% request.setAttribute("grade","78"); %> <c:choose> <c:when test="${requestScope.grade >= 85}"> <h2>优秀</h2> </c:when> <c:when test="${requestScope.grade < 85&&requestScope.grade >= 60}"> <h2>良好</h2> </c:when> <c:when test="${requestScope.grade < 60}"> <h2>不及格</h2> </c:when> <c:otherwise> <h2>成绩录入有问题</h2> </c:otherwise> </c:choose>

    结果:

    4.foreach

    foreach应用: <hr> <%-- 1.遍历1到10,输出 begin属性设置开始的索引 end 属性设置结束的索引 var 属性表示遍历循环的变量(当前正在遍历到的数据) --%> <c:forEach begin="1" end="10" var="i"> ${ i } </c:forEach> <hr> <%--2.遍历Object数组 for(Object var: items) items 属性表示遍历的数据源(遍历的集合) var 属性表示当前遍历的数据 --%> <% request.setAttribute("arr",new String[]{"张三","李四","王五","老幺"}); %> <c:forEach items="${requestScope.arr}" var="item"> ${item} </c:forEach> <hr> <%-- 3.遍历Map集合 --%> <% Map<String,Object> map = new HashMap<String,Object>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); request.setAttribute("map",map); %> <c:forEach items="${requestScope.map}" var="entry"> ${entry} </c:forEach> <hr> <%-- 4.遍历list集合, list集合里面是学生对象 (学生对象里有Id,username,password,age,phone) --%> <% List<Student> studentList = new ArrayList<Student>(); for(int i = 1;i <= 10; i++){ studentList.add(new Student(i,"username"+i,"password"+i,18+i,"phone"+i)); } request.setAttribute("list",studentList); %> <%-- 这里用表格来输出 --%> <table> <th>编号</th> <th>用户名</th> <th>密码</th> <th>年龄</th> <th>电话号</th> <c:forEach begin="0" end="9" step="2" items="${requestScope.list}" var="student"> <tr> <td>${student.id}</td> <td>${student.username}</td> <td>${student.password}</td> <td>${student.age}</td> <td>${student.phone}</td> <td>${status}</td> </tr> </c:forEach> </table> <hr>

    结果:

    Processed: 0.014, SQL: 8