Java JSTL标签案例怎么使用

wen java案例 21

本文目录导读:

Java JSTL标签案例怎么使用

  1. Java JSTL标签使用教程
  2. 环境配置
  3. 核心标签库(Core)常用标签
  4. 格式化标签(FMT)常用标签
  5. 完整案例
  6. 注意事项

Java JSTL标签使用教程

JSTL(JSP Standard Tag Library)是一组标准的JSP标签库,可以简化JSP页面的开发,下面详细介绍如何使用JSTL标签。

环境配置

1 添加依赖

Maven项目(pom.xml):

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

普通Web项目

  • 下载 jstl-1.2.jar 并放入 WEB-INF/lib 目录
  • 或使用Tomcat自带的jstl库

2 在JSP页面引入标签库

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

核心标签库(Core)常用标签

1 输出变量 - <c:out>

<!-- 基本用法 -->
<c:out value="${user.name}" default="未知用户"/>
<!-- 转义输出(防止XSS攻击) -->
<c:out value="${user.description}" escapeXml="true"/>

2 设置变量 - <c:set>

<!-- 设置属性 -->
<c:set var="username" value="张三" scope="session"/>
<c:set var="age" value="25"/>
<!-- 从请求参数获取 -->
<c:set var="searchKeyword" value="${param.keyword}"/>

3 条件判断 - <c:if>

<c:if test="${not empty userList}">
    <p>有用户数据</p>
</c:if>
<!-- 简单判断 -->
<c:if test="${user.age >= 18}">
    <p>成年人</p>
</c:if>

4 多条件判断 - <c:choose>

<c:choose>
    <c:when test="${score >= 90}">
        <p>优秀</p>
    </c:when>
    <c:when test="${score >= 80}">
        <p>良好</p>
    </c:when>
    <c:when test="${score >= 60}">
        <p>及格</p>
    </c:when>
    <c:otherwise>
        <p>不及格</p>
    </c:otherwise>
</c:choose>

5 循环遍历 - <c:forEach>

<!-- 遍历集合 -->
<c:forEach items="${users}" var="user" varStatus="status">
    <tr>
        <td>${status.index + 1}</td>
        <td>${user.name}</td>
        <td>${user.email}</td>
        <td>
            <c:if test="${status.first}">第一个</c:if>
            <c:if test="${status.last}">最后一个</c:if>
        </td>
    </tr>
</c:forEach>
<!-- 固定次数循环 -->
<c:forEach begin="1" end="5" var="i">
    <p>第${i}次循环</p>
</c:forEach>

6 URL处理 - <c:url>

<!-- 构建URL,自动添加上下文路径 -->
<a href="<c:url value='/user/detail'/>">用户详情</a>
<!-- URL重写(支持Session跟踪) -->
<c:url value="/user/edit" var="editUrl">
    <c:param name="id" value="${user.id}"/>
    <c:param name="action" value="edit"/>
</c:url>
<a href="${editUrl}">编辑</a>

格式化标签(FMT)常用标签

1 数字格式化

<!-- 格式化数字 -->
<fmt:formatNumber value="${price}" type="currency" currencySymbol="¥"/>
<fmt:formatNumber value="${salary}" pattern="#,##0.00"/>
<!-- 解析数字 -->
<fmt:parseNumber value="${inputNumber}" var="parsedNum"/>

2 日期格式化

<!-- 格式化日期 -->
<fmt:formatDate value="${now}" pattern="yyyy-MM-dd HH:mm:ss"/>
<fmt:formatDate value="${birthday}" type="date" dateStyle="long"/>
<!-- 解析日期 -->
<fmt:parseDate value="${dateString}" pattern="yyyy-MM-dd" var="parsedDate"/>

完整案例

用户管理页面示例

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>用户管理</title>
    <style>
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
        .pagination { margin: 20px 0; }
        .active { font-weight: bold; }
    </style>
</head>
<body>
    <h2>用户列表</h2>
    <!-- 搜索表单 -->
    <form action="<c:url value='/user/list'/>" method="get">
        <input type="text" name="keyword" value="${param.keyword}" placeholder="搜索用户..."/>
        <button type="submit">搜索</button>
    </form>
    <!-- 用户表格 -->
    <table>
        <thead>
            <tr>
                <th>序号</th>
                <th>用户名</th>
                <th>邮箱</th>
                <th>注册时间</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <c:choose>
                <c:when test="${empty users}">
                    <tr>
                        <td colspan="6">暂无用户数据</td>
                    </tr>
                </c:when>
                <c:otherwise>
                    <c:forEach items="${users}" var="user" varStatus="status">
                        <tr>
                            <td>${status.index + 1}</td>
                            <td>${user.name}</td>
                            <td>${user.email}</td>
                            <td>
                                <fmt:formatDate value="${user.registerDate}" 
                                    pattern="yyyy-MM-dd HH:mm"/>
                            </td>
                            <td>
                                <c:choose>
                                    <c:when test="${user.status == 1}">
                                        <span style="color:green">正常</span>
                                    </c:when>
                                    <c:when test="${user.status == 0}">
                                        <span style="color:gray">未激活</span>
                                    </c:when>
                                    <c:otherwise>
                                        <span style="color:red">禁用</span>
                                    </c:otherwise>
                                </c:choose>
                            </td>
                            <td>
                                <a href="<c:url value='/user/view'>
                                    <c:param name='id' value='${user.id}'/>
                                </c:url>">查看</a>
                                <a href="<c:url value='/user/edit'>
                                    <c:param name='id' value='${user.id}'/>
                                </c:url>">编辑</a>
                                <a href="<c:url value='/user/delete'>
                                    <c:param name='id' value='${user.id}'/>
                                </c:url>" onclick="return confirm('确认删除?')">删除</a>
                            </td>
                        </tr>
                    </c:forEach>
                </c:otherwise>
            </c:choose>
        </tbody>
    </table>
    <!-- 分页 -->
    <div class="pagination">
        <c:if test="${currentPage > 1}">
            <a href="<c:url value='/user/list'>
                <c:param name='page' value='${currentPage - 1}'/>
                <c:param name='keyword' value='${param.keyword}'/>
            </c:url>">&laquo; 上一页</a>
        </c:if>
        <c:forEach begin="1" end="${totalPages}" var="page">
            <c:choose>
                <c:when test="${page == currentPage}">
                    <span class="active">${page}</span>
                </c:when>
                <c:otherwise>
                    <a href="<c:url value='/user/list'>
                        <c:param name='page' value='${page}'/>
                        <c:param name='keyword' value='${param.keyword}'/>
                    </c:url>">${page}</a>
                </c:otherwise>
            </c:choose>
        </c:forEach>
        <c:if test="${currentPage < totalPages}">
            <a href="<c:url value='/user/list'>
                <c:param name='page' value='${currentPage + 1}'/>
                <c:param name='keyword' value='${param.keyword}'/>
            </c:url>">下一页 &raquo;</a>
        </c:if>
    </div>
</body>
</html>

注意事项

  1. EL表达式:JSTL通常与EL表达式配合使用,确保JSP页面启用EL
  2. 性能考虑:避免在JSTL标签中写复杂逻辑,保持视图简洁
  3. 作用域:注意变量作用域(page/request/session/application)
  4. 版本兼容:不同Servlet容器可能需要不同版本的jstl库

这样,你就可以在JSP页面中灵活使用JSTL标签来简化开发了!

抱歉,评论功能暂时关闭!