看我这边文章的人,请先去http://blog.csdn.net/qq598535...,完全学习完了,理解了,再过来我这边看看吧,反正我是刚从php转来Java的小白~~~,有可以改良的请告诉我哈!
- 首先是它最后没有做view层开始先把:
先回顾一下它最后做到web包下的BookController先:
@Autowired
private BookService bookService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
private String list(Model model) {
List<Book> list = bookService.getList();
model.addAttribute("list", list);
// list.jsp + model = ModelAndView
return "list";// WEB-INF/jsp/"list".jsp
}
这是他写的第一个方法,我把它稍微改成自己熟悉的写法,效果都是一样的:
@Autowired
private BookService bookService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
private ModelAndView list(ModelAndView mv) {
List<Book> list = bookService.getList();
for (Book book : list) {
System.out.println(book);
}
mv.addObject("list", list);
mv.setViewName("list");
return mv;
}
然后我写了list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HelloSpringMVC</title>
</head>
<body>
<table>
<c:forEach items="${list}" var="book" varStatus="vs">
<tr>
<td>
<s:property value="#vs.index+1"/>
</td>
<td align="center">${book.bookId}</td>
<td align="center">${book.name}</td>
<td align="center">${book.number}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
这里的jsp要学会jsp的循环list标签才可以做,这里推荐一篇文章:http://blog.csdn.net/kalision...
然后就可以扔进tomact去打开localhost:8080/你的项目名/book/list: