1. 程式人生 > >springMVC使用modelAndView返回介面與資料,jstl迴圈list

springMVC使用modelAndView返回介面與資料,jstl迴圈list

1.必要的maven依賴

<!-- JSP相關 -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.persistence</groupId>
      <artifactId>persistence-api</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-impl</artifactId>
      <version>1.2.5</version>
    </dependency>

2.後臺獲取的json資料格式

{
    "msg":"success",
    "code":0,
    "page":{
        "total":4,
        "list":[
            {
                "bname":"頒佈天數",
                "is_flag":"0",
                "bid":1,
                "type":"1",
                "bprice":"12",
                "bimg":"15407361597941231.jpg"
            },
            Object{...},
            Object{...},
            Object{...}
        ],
        "pageNum":1,
        "pageSize":8,
        "pages":1,
        "size":4
    }
}

3.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>
<head>
    <title>mybaties分頁</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<%--動態引入專案地址,方便部署--%>
<%
    String path = request.getContextPath();
    String ppath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path;
%>
<div style="width: 600px;height: 300px;background-color: whitesmoke">
    <table class="table table-striped">
        <thead>
        <tr>
            <th>書名</th>
            <th>價格</th>
            <th>分類</th>
            <th>圖片</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${requestScope.list}" var="book" >
            <tr>
                <td>${book.bname}</td>
                <td>${book.bprice}</td>
                <td>${book.type}</td><%--http://localhost:8080/onlineBooklib/statics/upload/--%>
                <td ><img style="overflow: hidden;width: 70px;height: 70px;" src="<%=basePath%>/upload/${book.bimg}"></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>
</body>
</html>

4.總結,開始以為jstl和Thymeleaf一樣,使用${list}就可以得到迴圈變數,並且誤以為items就是我要迴圈的那個變數,百度之後才知道 jstl的變數var=“key”
以下是對比:
Thymeleaf的迴圈

<tbody th:each="item : ${list}"   >
<tr>
<td><span th:text="${item.bname}"></span></td>
<td><span th:text="${item.bprice}"></span></td>
</tr>
</tbody>

jstl的迴圈

<c:forEach items="${requestScope.list}" var="book" >
            <tr>
                <td>${book.bname}</td>
                <td>${book.bprice}</td>
            </tr>
        </c:forEach>