JSTL快速入门

JSTL快速入门

  1. 导入坐标

    <!--jstl-->
     <dependency>
       <groupId>jstl</groupId>
       <artifactId>jstl</artifactId>
       <version>1.2</version>
     </dependency>
    
     <dependency>
       <groupId>taglibs</groupId>
       <artifactId>standard</artifactId>
       <version>1.1.2</version>
     </dependency>
    
  2. 在JSP页面上引入JSTL标签库

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

    • c:if

c:if标签

req.setAttribute("status", 1);
// 3. 转发到el-demo.jsp
//req.getRequestDispatcher("/el-demo.jsp").forward(req, resp);
req.getRequestDispatcher("/jstl-if.jsp").forward(req, resp);

对应的JSP文件

<%@ page contenType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
	<title>Title</title>
</head>
<body>
	<%--
        c:if: 来完成逻辑判断,替换java if else
    --%>
	<%--
		<c:if test="true">
	        <h1>true</h1>
	    </c:if>
	
	    <c:if test="false">
	        <h1>false</h1>
	    </c:if>--%>
	
	    <c:if test="${status == 1}">
	        启用
	    </c:if>
	
	    <c:if test="${status == 0}">
	        禁用
	    </c:if>
	   --%>
    
</body>
</html>

c:forEach标签

<table border="1" cellspacing="0" width="800">
    <tr>
        <th>序号</th>
        <th>品牌名称</th>
        <th>企业名称</th>
        <th>排序</th>
        <th>品牌介绍</th>
        <th>状态</th>
        <th>操作</th>

    </tr>
    <%--
	    <c:forEach items="${brands}" var="brand" varStatus="status">
	        <tr align="center">
	            <%--<td>${brand.id}</td>--%> <%--${brand.id}中的id表示的是实体类Brand中getId中的id,不是成员变量id。一般情况下命名是一致的--%>
	            <td>${status.count}</td>
	            <td>${brand.brandName}</td>
	            <td>${brand.companyName}</td>
	            <td>${brand.ordered}</td>
	            <td>${brand.description}</td>
	            <c:if test="${brand.status == 1}">
	                <td>启用</td>
	            </c:if>
	            <c:if test="${brand.status != 1}">
	                <td>禁用</td>
	            </c:if>
	            <td><a href="#">修改</a> <a href="#">删除</a></td>
	        </tr>
	    </c:forEach>
    --%>
</table>