我无法成功删除记录或数据。 我想我的问题在于ProductController.java。 帮助我摆脱这个错误。
<%@page session="false" %>
<%@page contentType="text/html;charset=UTF-8" %>
<%@page pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>:PharmaCloud System::</title>
<c:import url="../css.jsp"/>
</head>
<body>
<div class="container">
<h1>Products List</h1>
<div class="span-28 last">
<fieldset>
<c:forEach items="${Product}" var="products">
<p>Code : ${products.productid}</p>
<p>ISBN : ${products.productname}</p>
<p>Title : ${products.producttype}</p>
<ul>
<li>
<form:form action="../${products.productid}" method="GET">
<input alt="Detail" type="image" value="Detail"/>
</form:form>
<li><form:form action="../${products.productid}" method="DELETE">
<input alt="Delete" title="Delete Person" type="image"
value="Delete"/>
</form:form></li>
</ul>
</c:forEach>
<c:if test="${pageCount>1}">
<c:forEach begin="1" end="${pageCount}" var="page" step="1">
<a href="<c:url value="../list/${page}"/>">${page}</a>
</c:forEach>
</c:if>
</fieldset>
<ul>
<li><a href="<c:url value="/"/>">Main Menu</a></li>
<li><a href="<c:url value="/products"/>">Create Product</a></li>
</ul>
</div>
</div>
</body>
</html>
ProductController.java
package com.os.springjpa.controller;
import javax.validation.Valid;
import java.lang.*;
import org.apache.log4j.Logger;
import org.hibernate.type.LongType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.os.springjpa.dao.BookDao;
import com.os.springjpa.entity.Book;
import com.os.springjpa.utils.Utils;
/**
* @author kris;
* This class is using Path Variable style as example like Rest
* Style. Path Variable will handling like url string but so simple then
* url string. I'm also still not found how to make url string as simple
* as posible in view layer.
*/
@Controller
@RequestMapping("/book")
public class BookController {
@Autowired
private BookDao bookDao;
private final int PAGE_SIZE = 2;
Logger logger = Logger.getLogger(BookController.class);
@RequestMapping(method = RequestMethod.GET)
public String form(Model model) {
System.out.println(org.hibernate.Version.getVersionString());
model.addAttribute(new Book());
return "book/create";
}
@RequestMapping(method = RequestMethod.POST)
public String create(@Valid Book book, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
logger.info("Error has been detect");
return "book/create";
}
if (bookDao.saveOrUpdate(book)) {
book = bookDao.getByIsbn(book.getIsbn());
return "redirect:/book/" + book.getId();
} else {
return "/exception";
}
}
@RequestMapping(method = RequestMethod.PUT)
public String update(@Valid Book book, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
logger.info("Error has been detect");
return "book/update";
}
if (bookDao.saveOrUpdate(book)) {
return "redirect:/book/" + book.getId();
} else {
return "/exception";
}
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id) {
logger.info("Delete Method Has Call");
Book book = bookDao.getById(id);
if (bookDao.delete(book)) {
return "redirect:/book/list/1";
} else {
return "/exception";
}
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String detail(@PathVariable Integer id, Model model) {
Book book = bookDao.getById(id);
if (book == null) {
return "/exception";
}
model.addAttribute(book);
return "book/detail";
}
@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public String edit(@PathVariable Integer id, Model model) {
Book book = bookDao.getById(id);
if (book == null) {
return "/exception";
}
model.addAttribute(book);
return "book/update";
}
@RequestMapping(value = "/list/{page}", method = RequestMethod.GET)
public String listPaging(@PathVariable Integer page, Model model) {
// Create simple pagination
model.addAttribute("books", bookDao.getAll((page - 1) * PAGE_SIZE, PAGE_SIZE));
//model.addAttribute("pageCount", Utils.getPageCount(PAGE_SIZE, (Long) bookDao.countAllBook().get(0)));
model.addAttribute("pageCount", Utils.getPageCount(PAGE_SIZE, Long.parseLong(bookDao.countAllBook().get(0).toString())));
return "book/list";
}
}
答案 0 :(得分:0)
错误在事务bean里面...... 所以我放置了会话管理和事务管理
答案 1 :(得分:0)
代码设置了无法从数据库中删除的注释字段。