我一直在练习spring mvc,但在显示数据时遇到了问题。运行视图时,它根本不显示任何数据。这不是数据库连接的问题,因为登录工作正常。
home.jsp
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript"></script>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Store</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >
<style>
.container{
display:flex;
justfy-content: center;
}
</style>
</head>
<body>
<div class="jumbotron">
<div class="container text-center">
<h1>THE STORE</h1>
</div>
</div>
<div class="container-fluid bg-3 text-center">
<h2>Featured Products</h2>
<div class="row">
<c:forEach var="item" items="${items}">
<div class="col-sm-3">
<p> ${item.name} </p>
</div>
<br>
</c:forEach>
</div>
</div>
</body>
</html>
ItemController
package com.jj.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.jj.model.Item;
import com.jj.service.ItemService;
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping(value = {"/home"}, method= RequestMethod.POST)
public ModelAndView showItems(ModelAndView model, HttpServletRequest request) {
List<Item> items = itemService.findAllItems();
model.addObject("items", items);
model.setViewName("home.jsp");
return model;
}
}
ItemService
package com.jj.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.springframework.stereotype.Service;
import com.jj.db.DBPool;
import com.jj.model.Item;
@Service
public class ItemService {
private static Logger logger = Logger.getLogger(ItemService.class.getName());
//final static Logger logger = (Logger) LogManager.getLogger(ItemService.class);
public List<Item> findAllItems(){
logger.info("attempting to find all items");
ArrayList<Item> items = new ArrayList<Item>();
String sql = "SELECT * FROM " + Item.TABLE_NAME;
Connection conn = null;
DBPool.getInstance();
conn = DBPool.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
Item i = new Item();
i.setId(rs.getInt(Item.COLUMN_IDITEMS));
i.setName(rs.getString(Item.COLUMN_NAME));
items.add(i);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
logger.info("items " + items);
return items;
}
}
我一直在练习spring mvc,但在显示数据时遇到了问题。运行视图时,它根本不显示任何数据。这不是数据库连接的问题,因为登录工作正常。
答案 0 :(得分:0)
您需要taglib。我在下面附加了代码:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript"></script>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Store</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >
<style>
.container{
display:flex;
justfy-content: center;
}
</style>
</head>
<body>
<div class="jumbotron">
<div class="container text-center">
<h1>THE STORE</h1>
</div>
</div>
<div class="container-fluid bg-3 text-center">
<h2>Featured Products</h2>
<div class="row">
<c:forEach var="item" items="${items}">
<div class="col-sm-3">
<p> ${item.name} </p>
</div>
<br>
</c:forEach>
</div>
</div>
</body>
</html>
用于Spring Boot的配置JSP,您可以找到:example