最近在工作中需要做些前端的工作,当然还是比较low,使用的JSP。之前没做过,遇到了一些问题,这里记录下。
将后端传过来的List使用表格显示时,免不了要使用
<c:forEach>
</c:forEach>
要使用这个功能,需要在JSP文件头声明taglib。如果不声明taglib,使用开发者工具(alt+command+i)看返回的数据,发现没有将List展开。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
这样写了之后,如果不在工程的pom中引入相关的jar包依赖的话会有下面的问题。
HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
引入的依赖为:
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
下面是test.jsp的完整代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<div class="row">
<form class="form-inline" action="/test">
<div class="form-group">
<label class="label-middle">开始时间</label><input size="16" type="text" value="${startTime}" name="startTime">
<label class="label-middle">结束时间</label><input size="16" type="text" value="${endTime}" name="endTime">
<button type="submit">查询</button>
</div>
</form>
</div>
<div class="row margin-top-20">
<table class="table">
<thead>
<tr>
<th class="seq">序号</th>
<th>时间</th>
<th>MSG</th>
</tr>
</thead>
<tbody>
</tbody>
<c:forEach var="data" items="${datas}" varStatus="loop">
<tr>
<td>${loop.index + 1}</td>
<td>${data.time}</td>
<td>${data.msg}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>