Continue the previous content:
Whether the test framework is OK
Test path jump, whether the mapping address is correct
Front-end new folder: jsp, js, image
The Jsp folder places the jsp page
Add in the Web.xml file: < 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" > Enter user management page </ a ></ h5 >
</body>
</html>
Newly created under the Web package: 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";
}
}
Newly created under the Jsp folder: login.Jsp
(The reason why the path here can be directly jumped: spring-mvc.xml configuration: view parser, front and back, can be directly intercepted )
<%@ 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 " > Manage Books </a> </p> _ _ _ _ _ _ _
</body>
</html>
The result is as follows:
Next, test whether the before and after of the book work properly:
With the above path: < p >< a href = " <%= basePath %> book/getAllBook.do" > manage books </ a ></ p >
Create BookWeb.java under the web package
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/" ;//Basic path of front-end jsp page
@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";
}
}
Create a new book folder under the jsp folder to place the addition, deletion, modification and query of the 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>
operation result:
Reason for error:
The Book in BookDaoImpl was written as a lowercase book before
Replace book in the hql statement in BookDaoImpl with Book
String hql = "from book u where u.id=?";
Modify 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);
}
}