重新学习JSP之十九——JSTL

一.为什么需要JSTL

在大型项目开发中,系统架构师会使用分层的思想设计项目,处于表示层的JSP页面的功能就是显示数据。如果嵌入大量java代码,对于不熟悉java编程的网页设计师来说是件麻烦事,不利于项目的开发。

二.什么是JSTL

JSTL,中文为JSP标准标签库。可以应用于基本输入输出,流程控制等等。JSTL提供的标签库主要分为五大类,如下:

名称推荐前缀URI范例
核心标签库chttp://java.sun.com/jsp/jstl/core<c:out>
I18N标签库fmthttp://java.sun.com/jsp/jstl/fmtfmt:formatDate
SQL标签库sqlhttp://java.sun.com/jsp/jstl/sqlsql:query
XML标签库xhttp://java.sun.com/jsp/jstl/xml<x:forBach>
函数标签库fnhttp://java/sun.com/jsp/jstl/functionsfn:split

使用JSTL标签库,必须使用taglib指令,以core标签库为例,语法如下:

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

taglib是指令,prefix是前缀,该代码是引入core标签库,并指定标签的前缀。

三.核心标签库

3.1什么是核心标签库

核心标签库,又称core标签库,该库包括与变量,控制流以及访问基于URL的资源相关的标签。

3.2用核心标签库进行基本操作

3.2.1<c:out>

作用是显示内容,语法如下:

<c:out value = "显示的内容"></c:out>

例子:
outExample.java

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>outExample</title>
  </head>
  
  <body>
         <%
             session.setAttribute("msg", "hello world!");
          %>
          <c:out value = "${msg}">
          </c:out>
  </body>
</html>

3.2.2<c:set>

作用是对变量或者JavaBean中的变量属性赋值。语法如下:

<c:set></c:set>

例子:
sexExample.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>setExample</title>
  </head>
  
  <body>
        <c:set value = "这是示例" scope = "session" var = "msg"><!-- 将这是示例添加到session里面,命名为msg -->
        </c:set>
        <c:out value = "${msg}">
        </c:out>
  </body>
</html>

3.2.3<c:remove>

作用是删除scope中的变量

<c:remove></c:remove>

例子:
removeExample.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>removeExample</title>
  </head>
  
  <body>
           <%
           session.setAttribute("msg", "这是示例");
         %>
         <c:remove var = "msg" scope = "session">
         </c:remove>
         <c:out value = "${msg}">
         </c:out>
  </body>
</html>

3.3用核心标签库进行选择流程

3.3.1<c:if>

基本语法是:

<c:if test = "${判断条件}">
...
</c:if>

该标签里面还有var和scope属性,代表判断结果的变量名,以及变量所在的范围。

例子:
ifExample.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<html>
  <head>
    <title>ifExample</title>
  </head>
  
  <body>
         <%
            session.setAttribute("score", 95);
          %>
          <c:if test = "${score >= 80 }">优秀</c:if>
          <c:if test = "${score <80 }">不优秀</c:if>
  </body>
</html>

3.3.2<c:choose><c:when><c:otherwise>

这三个标签通常一起用,类似于“if。。。else if”语句,基本语法如下:

<c:choose>
          <c:when test = "${条件1}"></c:when>
          <c:when test = "${条件2}"></c:when>
          <c:when test = "${条件N}"></c:when>
          <c:otherwise></c:otherwise>
</c:choose>

3.4用核心标签库实现循环流程

3.4.1<c;forEach>

功能是将集合中的成员顺序浏览一遍,基本语法如下:

<c:forEach var = "元素名" items = "集合名" begin = "起始" end = "结束" step = "步长"></c:forEach>

例子:

<c:forEach var = "stu" items = "${stus}">
     ${stu}
 </c:forEach>

表示将stus集合进行遍历,每个元素起名为stu,显示出来。

例子:
forEachExample

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import = "java.util.*" %>
<html>
  <head>
    <title>forEachExample</title>
  </head>
  
  <body>
        <%
            ArrayList al = new ArrayList();
            al.add("张三");
            al.add("张四");
            al.add("李四");
            session.setAttribute("stus", al);
         %>
         <a href = "forEachExample2.jsp">到达forEachExample2.jsp</a>
  </body>
</html>

forEachExample2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<html>
  <head>
    <title>forEachExample2</title>
  </head>
  
  <body>
          打印ArrayList中的内容:<hr>
        <c:forEach items = "${stus}" var = "stu">
                   ${stu }
              </c:forEach>
  </body>
</html>

其他标签库使用的较少,此处不作详述。

摘自《Java Web 开发与应用》,主编郭克华,副主编宋虹,清华大学出版社。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值