4.jstl第三方标签库的使用步骤及常用标签

tech2022-09-25  110

(1)引入jar包,依赖(找maven的网址:maven.aliyun.com) 将maven依赖引入到pom.xml中

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

(2)jsp页面中使用taglib指令引入 标签的开始一般是以c开头的,prefix=“c”,uri用提示补齐

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

(3)常用标签使用

1.循环标签 <c:forEach> 必须属性 item=${域对象中的值} var="临时的每次遍历的对象" 可选属性 begin="0" //默认是从0开始的 end="" //想让遍历几次 常见错误: item="${map} " 右花括号}和右"之间不能存在空格 <c:forEach begin="0" end="3" items="${requestScope.list}" var="student"> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.age}</td> </tr> </c:forEach> 2.输出标签 <c:out> 一般我们直接使用EL表达式展示 代码: <c:out value="${name }"></c:out> <c:out value="${1+1}"></c:out> 3.存值标签 <c:set> 4.移除标签 <c:remove> <c:set var="name" value="tom" scope="request"></c:set> <c:remove var="name" scope="request" /> 一般存值,移除值在servlet中操作 代码: session.setAttribute("name", "Tom"); //移除域对象中的值 session.removeAttribute("name"); response.sendRedirect("jstl.jsp"); -----------------------------jsp-------------------------- <c:set var="name" value="tom" scope="request"></c:set> <c:remove var="name" scope="request"/> 5.条件判断标签 <c:if> <c:if test="${1==1 }">true</c:if> 6.逻辑判断标签 组合标签,不能单独使用 <c:choose> <c:when> <c:otherwise> 相当于if(){} elseif(){} elseif(){} else{}

移除域对象中的值removeAttribute

session.setAttribute(“name”, “Tom”); session.removeAttribute(“name”);

先写一个静态网页

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <center> <table border="1px"> <tr> <th>学号</th> <th>姓名</th> <th>年龄</th> </tr> <tr> <td>1</td> <td>张三</td> <td>20</td> </tr> <tr> <td>2</td> <td>王五</td> <td>18</td> </tr> </table> </center> </body> </html>

主要目标:是将表中的张三,王五等数据换成动态的数据。。。。。 Student.java

public class Student { private Long id; private String name; private Integer age; private String password; set... get... }

循环标签<c:forEach>->list

ValueServlet.java,将list里面的值让jstl.jsp读取

@WebServlet("/value") public class ValueServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //servlet存值,jsp读取值并显示 //如果用request存值,必须内部跳转 ArrayList<Student> list=new ArrayList<>(); list.add(new Student(1L, "tom1", 12)); list.add(new Student(2L, "tom2", 18)); list.add(new Student(3L, "tom3", 21)); list.add(new Student(4L, "tom4", 25)); request.setAttribute("list", list); /* * 把刚才上面list里面的值让jstl.jsp读取 * */ //内部跳转 request.getRequestDispatcher("jstl.jsp").forward(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

jstl.jsp主要内容

<body> <center> <table border="1px"> <tr> <th>学号</th> <th>姓名</th> <th>年龄</th> </tr> <!-- 把ValueServlet.java里面的list值,让jstl.jsp读取 --> <!-- 通过EL表达式取list中的对象 var="每次遍历的对象" begin="0" //默认是从0开始的 end=""//想让遍历几次--> <c:forEach begin="0" end="3" items="${requestScope.list}" var="student"> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.age}</td> </tr> </c:forEach> </table> </center> </body>

循环标签<c:forEach>->map

ValueServlet.java,将map里面的值让jstl.jsp读取

@WebServlet("/value") public class ValueServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //session HttpSession session = request.getSession(); HashMap<Long, Student> map=new HashMap<>(); map.put(1L, new Student(1L, "tom1", 13)); map.put(2L, new Student(2L, "tom2", 15)); map.put(3L, new Student(3L, "tom3", 17)); map.put(4L, new Student(4L, "tom4", 19)); session.setAttribute("map", map); /* * 把刚才上面map里面的值让jstl.jsp读取 * */ response.sendRedirect("jstl.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

jstl.jsp主要内容

<body> <center> <table border="1px"> <tr> <th>学号key</th> <th>学号value</th> <th>姓名</th> <th>年龄</th> </tr> <!-- 把ValueServlet.java里面的map值,让jstl.jsp读取 --> <c:forEach items="${map}" var="entry"> <tr> <td>${entry.key}</td> <td>${entry.value.id}</td> <td>${entry.value.name}</td> <td>${entry.value.age}</td> </tr> </c:forEach> </table> </center> </body>

输出标签 <c:out>

ValueServlet.java,将map里面的值让jstl.jsp读取

@WebServlet("/value") public class ValueServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.setAttribute("name", "Tom"); response.sendRedirect("jstl.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

jstl.jsp主要内容

<body> <center> <c:out value="${name }"></c:out> <br> <c:out value="${1+1}"></c:out> </center> </body>

存值标签 <c:set> 移除标签 <c:remove>

一般存值,移除值在servlet中操作 <c:set var="name" value="tom" scope="request"></c:set> <c:remove var="name" scope="request" /> 代码: session.setAttribute("name", "Tom"); //移除域对象中的值 session.removeAttribute("name");

条件判断标签 <c:if test=“布尔表达式”>

ValueServlet.java

@WebServlet("/value") public class ValueServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.setAttribute("student", new Student(4L, "Jerry", 19)); response.sendRedirect("jstl.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } <body> <center> <c:if test="${1==1 }">true</c:if><br> <c:if test="${student==null }">请登录</c:if><br> <c:if test="${student!=null }">欢迎你,${student.name }</c:if><br> <c:if test="${empty student }">请登录</c:if><br> <c:if test="${not empty student }">欢迎你,${student.name }</c:if> </center> </body>

逻辑判断标签<c:choose><c:when><c:otherwise>

组合标签,不能单独使用 相当于if(){} elseif(){} elseif(){} else{}

ValueServlet.java

@WebServlet("/value") public class ValueServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); session.setAttribute("score", 88); response.sendRedirect("jstl.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } <body> <center> <c:choose> <c:when test="${score>=90 }"></c:when> <c:when test="${score>=80 }"></c:when> <c:when test="${score>=70 }"></c:when> <c:when test="${score>=60 }">及格</c:when> <c:otherwise></c:otherwise> </c:choose> </center> </body>

最新回复(0)