分页
package com.library.controller;
import com.library.bean.Book;
import com.library.bean.Lend;
import com.library.bean.ReaderCard;
import com.library.service.BookService;
import com.library.service.LendService;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@Autowired
private LendService lendService;
private Date getDate(String pubstr) {
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.parse(pubstr);
} catch (ParseException e) {
e.printStackTrace();
return new Date();
}
}
@RequestMapping("/querybook.html")
public ModelAndView queryBookDo(String searchWord) {
if (bookService.matchBook(searchWord)) {
ArrayList<Book> books = bookService.queryBook(searchWord);
ModelAndView modelAndView = new ModelAndView("admin_books");
modelAndView.addObject("books", books);
return modelAndView;
} else {
return new ModelAndView("admin_books", "error", "没有匹配的图书");
}
}
@RequestMapping("/reader_querybook_do.html")
public ModelAndView readerQueryBookDo(String searchWord) {
if (bookService.matchBook(searchWord)) {
ArrayList<Book> books = bookService.queryBook(searchWord);
ModelAndView modelAndView = new ModelAndView("reader_books");
modelAndView.addObject("books", books);
return modelAndView;
} else {
return new ModelAndView("reader_books", "error", "没有匹配的图书");
}
}
@RequestMapping("/admin_books.html")
public ModelAndView adminBooks() {
ArrayList<Book> books = bookService.getAllBooks();
ModelAndView modelAndView = new ModelAndView("admin_books");
modelAndView.addObject("books", books);
return modelAndView;
}
@RequestMapping("/book_add.html")
public ModelAndView addBook() {
return new ModelAndView("admin_book_add");
}
@RequestMapping("/book_add_do.html")
public String addBookDo(@RequestParam(value = "pubstr") String pubstr, Book book, RedirectAttributes redirectAttributes) {
book.setPubdate(getDate(pubstr));
if (bookService.addBook(book)) {
redirectAttributes.addFlashAttribute("succ", "图书添加成功!");
} else {
redirectAttributes.addFlashAttribute("succ", "图书添加失败!");
}
return "redirect:/admin_books.html";
}
@RequestMapping("/updatebook.html")
public ModelAndView bookEdit(HttpServletRequest request) {
long bookId = Long.parseLong(request.getParameter("bookId"));
Book book = bookService.getBook(bookId);
ModelAndView modelAndView = new ModelAndView("admin_book_edit");
modelAndView.addObject("detail", book);
return modelAndView;
}
@RequestMapping("/book_edit_do.html")
public String bookEditDo(@RequestParam(value = "pubstr") String pubstr, Book book, RedirectAttributes redirectAttributes) {
book.setPubdate(getDate(pubstr));
if (bookService.editBook(book)) {
redirectAttributes.addFlashAttribute("succ", "图书修改成功!");
} else {
redirectAttributes.addFlashAttribute("error", "图书修改失败!");
}
return "redirect:/admin_books.html";
}
@RequestMapping("/admin_book_detail.html")
public ModelAndView adminBookDetail(HttpServletRequest request) {
long bookId = Long.parseLong(request.getParameter("bookId"));
Book book = bookService.getBook(bookId);
ModelAndView modelAndView = new ModelAndView("admin_book_detail");
modelAndView.addObject("detail", book);
return modelAndView;
}
@RequestMapping("/reader_book_detail.html")
public ModelAndView readerBookDetail(HttpServletRequest request) {
long bookId = Long.parseLong(request.getParameter("bookId"));
Book book = bookService.getBook(bookId);
ModelAndView modelAndView = new ModelAndView("reader_book_detail");
modelAndView.addObject("detail", book);
return modelAndView;
}
@RequestMapping("/admin_header.html")
public ModelAndView admin_header() {
return new ModelAndView("admin_header");
}
@RequestMapping("/reader_header.html")
public ModelAndView reader_header() {
return new ModelAndView("reader_header");
}
public String refreshUrl(int currentPage,int total) {
int pagesize = 3;
int allPage = total/pagesize;
if((total % pagesize)!=0){
allPage = allPage +1;
}
StringBuffer buf = new StringBuffer();
buf.append("<font color='#1157B7'>共").append(total);
buf.append("条");
buf.append(" ");
buf.append("第").append(currentPage).append("/").append(allPage).append("页");
buf.append(" ");
if (currentPage == 1){
buf.append("首页");
}else{
buf.append("<a href='").append("/LIB/reader_books.html?1=1").append("¤tPage=1")
.append("")
.append("' class='ls'>").append("首页")
.append("</a>");
}
buf.append(" ");
if (currentPage > 1) {
buf.append("<a href='").append("/LIB/reader_books.html?1=1").append("¤tPage=")
.append(currentPage - 1).append("").append(
"' class='ls'>").append("上页")
.append("</a>");
} else {
buf.append("上页");
}
buf.append(" ");
if (currentPage < allPage) {
buf.append("<a href='").append("/LIB/reader_books.html?1=1").append("¤tPage=")
.append(currentPage + 1).append("").append(
"' class='ls'>").append("下页")
.append("</a>");
} else {
buf.append("下页");
}
buf.append(" ");
if (currentPage == allPage){
buf.append("末页");
}else{
buf.append("<a href='").append("/LIB/reader_books.html?1=1").append("¤tPage=")
.append(allPage).append("").append(
"' class='ls'>").append("末页")
.append("</a></font>");
}
buf.append(" ");
buf.append("<select onchange=\"javascript:window.location='").append(
"/LIB/reader_books.html?1=1").append("¤tPage='+").append(
"this.options[this.selectedIndex].value").append("")
.append("\">");
for (int i = 0; i < allPage; i++) {
if (currentPage == i + 1)
buf.append("<option value=" + (i + 1)
+ " selected=\"selected\">" + (i + 1) + "</option>");
else
buf.append("<option value=" + (i + 1) + ">" + (i + 1)
+ "</option>");
}
buf.append("</select>");
System.out.print(buf.toString());
return buf.toString();
}
@RequestMapping("/reader_books.html")
public ModelAndView readerBooks(HttpServletRequest request) {
ArrayList<Book> books = bookService.getAllBooks();
String currentPage = request.getParameter("currentPage");
if(currentPage == null){
currentPage = "1";
}
int currPage_int = new Integer(currentPage).intValue();
int allTotal = bookService.getAllBooksPageTotal(currPage_int,currPage_int);
books = bookService.getAllBooksPage(currPage_int,3);
ReaderCard readerCard = (ReaderCard) request.getSession().getAttribute("readercard");
ArrayList<Lend> myAllLendList = lendService.myLendList(readerCard.getReaderId());
ArrayList<Long> myLendList = new ArrayList<>();
for (Lend lend : myAllLendList) {
// 是否已归还
if (lend.getBackDate() == null) {
myLendList.add(lend.getBookId());
}
}
ModelAndView modelAndView = new ModelAndView("reader_books");
modelAndView.addObject("books", books);
modelAndView.addObject("myLendList", myLendList);
modelAndView.addObject("refreshUrl", refreshUrl(currPage_int,allTotal));
return modelAndView;
}
}

浙公网安备 33010602011771号