本节讲述标准标签库jstl的用法,其主要作用就是用标签代替java代码,当然底层还是java代码。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/jstl")
public class JSTLServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<String> list = new ArrayList<>();
list.add("ali");
list.add("xiaoli");
list.add("zhangli");
req.setAttribute("list", list);
req.setAttribute("role", "admin");
req.getRequestDispatcher("jstl.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:forEach items="${list}" var="user">
<c:out value="${user}"></c:out>
</c:forEach>
<c:if test="${role == ‘admin‘}">管理员</c:if>
</body>
</html>
原文:https://www.cnblogs.com/alichengxuyuan/p/12622657.html