侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 747 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

JavaWeb(11)之JSTL标签库-Java Standard Tag Libiary

zze
zze
2017-06-18 / 0 评论 / 0 点赞 / 418 阅读 / 9989 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

介绍

JSTL 是一个不断完善的开放源代码的 JSP 标签库。
它的作用是简化 JSP 中代码中 Java 代码的编写,即替换 <% %> 写法,一般与 EL 表达式配合使用。

使用

1、导包:jstl.jarstandard.jar
2、在 JSP 中使用 taglib 指令引入标签库。

如果想要支持 EL 表达式,那么引入的标签库版本要在 1.1 以上。

常用标签

下面列出几个比较常用的标签:

set

作用:将指定 key-value 值存放到域对象。

<!-- set.jsp -->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>set</title>
</head>
<body>
<%--
参数:
    var:名称
    value:值
    scope:默认为 page ,还有 request、session、application 选项
        为 page 时,相当于 <% pageContext.setAttribute("名称","值"); %>
        为 request 时,相当于 <% request.setAttribute("名称","值"); %>
        为 session 时,相当于 <% session.setAttribute("名称","值"); %>
        为 application 时,相当于 <% application.setAttribute("名称","值"); %>
--%>
<c:set var="age" value="18" scope="page"></c:set>
${age}
</body>
</html>

image.png

if

作用:简单的 if 条件判断。

<!-- if.jsp -->
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>if</title>
</head>
<body>
<c:set var="age" value="18" scope="page"></c:set>
<%--
参数:
    test:条件表达式
    var:指定变量接收条件表达式结果
    scope:标识条件表达式中的变量从哪个域中取
下面代码相当于:
--%>
<%
    Integer age = Integer.parseInt(pageContext.getAttribute("age").toString());
    boolean flag = age >= 18;
    pageContext.setAttribute("flag", flag);
    if (flag) {
%>
成年人
<%
    }
%>

<c:if test="${age>=18}" var="flag" scope="page">
成年人
</c:if>
${flag}

image.png

foreach

作用:循环。

<!-- forEach.jsp -->
<%@ page import="com.zze.bean.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>forEach</title>
</head>
<body>
<%--
for 循环
参数:
    begin:起始数字
    end:结束数字
    var:定义一个变量代指当前遍历对象
    step:增幅
--%>
<c:forEach begin="1" end="10" var="item" step="2">
    ${item}
</c:forEach>
<%
    List<User> userList = new ArrayList<User>();
    userList.add(new User("张三", 18));
    userList.add(new User("李四", 19));
    userList.add(new User("王五", 20));
    pageContext.setAttribute("userList", userList);
%>
<hr>
<%--
foreach 循环
参数:
    begin:起始下标从 0 开始
    end:结束下标
    step:下标增幅
    var:定义一个变量代指当前遍历对象
    items:指定要遍历的可迭代对象
    varStatus:定义一个保存了当前遍历信息的对象,有如下属性:
        begin:当前遍历的起始下标从 0 开始
        end:当前遍历的结束下标
        step:当前遍历的下标增幅
        count:当前遍历的是第几个,从 1 开始
        current:代指当前遍历对象,和 var 定义的变量指向同一个地址
        first:当前遍历对象是否是第一个
        last:当前遍历对象是否是最后一个
        index:当前遍历对象的下标
--%>
<c:forEach begin="0" end="${userList.size()}" step="2" var="user" items="${userList}" varStatus="s">
    user.name:${user.name},user.age:${user.age},s.step:${s.step},s.begin:${s.begin},s.end:${s.end},s.count:${s.count},s.current:${s.current},s.current == user:${s.current == user},s.first:${s.first},s.last:${s.last}<br>
</c:forEach>
</body>
</html>

image.png

自定义标签库

最简单的标签

1、创建一个标签处理类,该类需要继承 SimpleTagSupport 类,重写 doTag 方法:

// com.zze.tag.HelloTag
package com.zze.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class HelloTag extends SimpleTagSupport {

    @Override
    public void doTag() throws JspException, IOException {
        getJspContext().getOut().print("<h1>Hello My Tag!!!</h1>");
    }
}

2、在 WEB-INF 下创建标签库配置文件:

<!-- WEB-INF/tld/mytag.tld -->
<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <description>tag created by zze</description>
    <tlib-version>1.0</tlib-version>
    <short-name>myTag</short-name>
    <!--标签库唯一标识-->
    <uri>zze.test</uri>
    <tag>
        <description>Outputs Hello</description>
        <!--标签名称-->
        <name>helloTag</name>
        <tag-class>com.zze.tag.HelloTag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>

3、在 JSP 页面中使用:

<!-- index.jsp -->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="mytag" uri="zze.test"%>
<html>
  <head>
    <title>自定义标签库测试</title>
  </head>
  <body>
  <mytag:helloTag/>
  </body>
</html>

image.png

自定义带属性的标签

1、创建一个标签处理类,声明一个属性并给它 setter,重写 doTag 方法:

// com.zze.tag.TableTag
package com.zze.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.HashMap;

public class TableTag extends SimpleTagSupport {
    private String map;

    public void setMap(String map) {
        this.map = map;
    }

    @Override
    public void doTag() throws JspException, IOException {
        HashMap<String, Integer> maps = (HashMap<String, Integer>) (getJspContext().getAttribute(map));
        Object[] array = maps.keySet().toArray();

        for (String str : maps.keySet()) {
            getJspContext().getOut().write("<tr>");
            getJspContext().getOut().write("<td>");
            getJspContext().getOut().write(str);
            getJspContext().getOut().write("</td>");
            getJspContext().getOut().write("<td>");
            getJspContext().getOut().write("" + maps.get(str));
            getJspContext().getOut().write("</td>");
            getJspContext().getOut().write("</tr>");
        }
    }
}

2、在 WEB-INF 下创建标签库配置文件:

<!-- WEB-INF/tld/mytag.tld -->
<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <description>tag created by zze</description>
    <tlib-version>1.0</tlib-version>
    <short-name>myTag</short-name>
    <uri>zze.test</uri>

    <tag>
        <name>tr</name>
        <tag-class>com.zze.tag.TableTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <!--声明一个属性-->
            <name>map</name>
            <required>true</required>
            <fragment>true</fragment>
        </attribute>
    </tag>
</taglib>

3、在 JSP 页面中使用:

<!-- index.jsp -->
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="mytag" uri="zze.test" %>
<html>
<head>
    <title>自定义标签库测试</title>
</head>
<body>
<%
    HashMap<String, Integer> maps = new HashMap<String, Integer>();
    maps.put("张三", 18);
    maps.put("李四", 23);
    maps.put("王五", 23);
    pageContext.setAttribute("map", maps);
%>
<table border="1">
    <mytag:tr map="map"/>
</table>
</body>
</html>

image.png

0

评论区