ssm整合——查询书籍功能
package com.zy.controller;
import com.zy.pojo.Books;
import com.zy.service.BookService;
import com.zy.service.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/book")
public class BookController {
/*controller 层 调用 service层*/
@Autowired
@Qualifier("BookServiceImpl")
private BookService bookService;
/*查询全部的书籍,并且返回到一个书籍展示页面*/
@RequestMapping("/allBook")
public String list(Model model){
List<Books> list = bookService.allbook();
model.addAttribute("list", list);
return "allBook";
}
}
编写前端代码
index.jsp 首页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
<style>
h3{
width: 180px;
height: 38px;
margin: 100px auto;
text-align: center;
line-height: 38px;
background: skyblue;
border-radius: 50px ;
}
a{
text-decoration: none;
color: black;
font-size: 18px;
}
</style>
</head>
<body>
<h3>
<a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面</a>
</h3>
</body>
</html>
allbooks .jsp 跳转的书籍页面
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍展示页面</title>
<%--BootStrap美化界面--%>
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<samp>书籍列表---显示所有书籍</samp>
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名称</th>
<th>书籍数量</th>
<th>书籍详情</th>
</tr>
</thead>
<tbody>
<%--书籍从数据库中查询出来,
从这个list中遍历出来:foreach--%>
<c:forEach var="book" items="${list}">
<tr>
<td>${book.bookid}</td>
<td>${book.bookname}</td>
<td>${book.bookcount}</td>
<td>${book.dateail}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
显示的结果为
正在学习中,有错误的地方,请多多指教!