接前面的內容:
測試框架搭建是否OK
測試路徑跳轉,映射地址是否正確
前端新建文件夾:jsp,js,image
Jsp文件夾放置jsp頁面
Web.xml文件中增加:<welcome-file>index.jsp</welcome-file>
Index.jsp
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%Stringpath = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 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>Insert title here</title>
</head>
<body>
<h5><a href="<%=basePath%>user/login.do">進入用戶管理頁</a></h5>
</body>
</html>
Web包下新建:User.java
package com.test.web;
importjavax.servlet.http.HttpServletRequest;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class User {
//private String basePath="/jsp";
@RequestMapping("/login")
public String getAllUser(HttpServletRequest request){
System.out.println("*****************************aaaaa");
return "/login";
}
}
Jsp文件夾下新建:login.Jsp
(這裏的路徑可以直接跳轉的原因:spring-mvc.xml配置:視圖解析器,前置和後置,可以直接攔截到)
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 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>Insert title here</title>
</head>
<body>
<p><a href="<%=basePath%>book/getAllBook.do">管理書籍</a></p>
</body>
</html>
結果如下:
接下來測試book的前後是否能正常工作:
有以上路徑:<p><a href="<%=basePath%>book/getAllBook.do">管理書籍</a></p>
在web包下建立BookWeb.java
package com.test.web;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.test.manager.BookManager;
@Controller
@RequestMapping("/book")
public classBookWeb {
private String basePath="/book/";//前臺jsp頁面的基本路徑
@Resource(name="bookManager")
privateBookManager bookManager;
@RequestMapping("/getAllBook")
publicString getAllUser(HttpServletRequest request){
System.out.println("*****************************getAllBook");
System.out.println("bookManager.getAllBook()--------"+bookManager.getAllBook());
request.setAttribute("bookList", bookManager.getAllBook());
return basePath+"book_list";
}
}
在jsp文件夾下新建book文件夾放置book的增刪改查
booklist.jsp
<table >
<c:if test="${!empty bookList }">
<thead>
<tr>
<th data-options="field:'bookname',width:200">bookname</th>
<th data-options="field:'auther',width:100">auther</th>
<th data-options="field:'isbn',width:100">isbn</th>
<th data-options="field:'price',width:100">price</th>
<th data-options="field:'stock',width:100">stock</th>
</tr>
</thead>
<c:forEach items="${bookList }" var="book">
<tr>
<td>${book.bookname }</td>
<td>${book.auther }</td>
<td>${book.isbn }</td>
<td>${book.price }</td>
<td>${book.stock }</td>
</tr>
</c:forEach>
</c:if>
</table>
運行結果:
報錯原因:
是在之前將BookDaoImpl 中的Book 寫成了小寫的book
將BookDaoImpl 中hql 語句中的book 換成Book
String hql = "from book u where u.id=?";
修改BookDaoImpl.java
package com.test.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import com.test.entity.Book;
public class BookDaoImpl implements BookDao{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
publicBook getBook(String id) {
//TODO Auto-generated method stub
String hql = "from Book u where u.id=?";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, id);
return (Book)query.uniqueResult();
}
@Override
publicList<Book> getAllBook() {
//TODO Auto-generated method stub
String hql = "from Book";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
return query.list();
}
@Override
publicvoid addBook(Book book) {
//TODO Auto-generated method stub
sessionFactory.getCurrentSession().save(book);
}
@Override
publicboolean delBook(String id) {
//TODO Auto-generated method stub
String hql = "delete Book u where u.id = ?";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, id);
return (query.executeUpdate() > 0);
}
@Override
publicboolean updateBook(Book book) {
//TODO Auto-generated method stub
String hql = "update Book u set u.bookname =?,u.isbn=?,u.price=?,u.stock=? where u.id = ?";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setString(0, book.getAuther());
query.setString(1, book.getBookname());
query.setString(2, book.getIsbn());
query.setInteger(3, book.getPrice());
query.setInteger(4, book.getStock());
return (query.executeUpdate() > 0);
}
}