我在春季3有弹簧数据(jpa)。当我尝试从产品的db详细信息加载时,服务器会给出消息:HTTP Status 500 - 服务器遇到内部错误,导致无法完成此请求。我可以在db中写入数据(没问题)并显示所有产品但不能显示详细信息。我不知道发生了什么(我尝试在tomcat 7.042和VMware v 2.9上同样的结果)。这是我的代码: 实体:
@Entity
@Table(name="product")
@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
public Long id;
private String author;
@Lob
private byte[] cover;
private String description;
private float price;
private String title;
private String year;
public Product() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
public byte[] getCover() {
return this.cover;
}
public void setCover(byte[] cover) {
this.cover = cover;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return this.price;
}
public void setPrice(float price) {
this.price = price;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getYear() {
return this.year;
}
public void setYear(String year) {
this.year = year;
}
}
存储库接口:
public interface ProductRepository extends JpaRepository<Product, Long>,PagingAndSortingRepository<Product, Long> {
@Query("SELECT p FROM Product p")
public Page<Product> takeAll( Pageable pageable);
@Query("select p from Product p where p.id = ?1")
public Product takeByTitle( Long id);
}
DAO:
@Service
@Transactional
public class ProductDAO {
@Autowired
ProductRepository productRepository;
public Page<Product> showAllProducts(Pageable pageable){
return productRepository.takeAll(pageable);
}
public Product searchByDetails(Long id){
return productRepository.takeByTitle(id);
}
}
端点:
@Component
@Transactional
public class ProductEndpoint {
@Autowired
ProductDAO productDAO;
public Page<Product> getAll(Pageable pageable){
return productDAO.showAllProducts(pageable);
}
public Product TakeDetails(Long id){
return productDAO.searchByDetails(id);
}
}
控制器:
@Controller
@RequestMapping("/index")
public class IndexController {
@Autowired
ProductEndpoint productEndpoint;
@RequestMapping(method = RequestMethod.GET)
public String showHome(Model model, Pageable pageable){
Page<Product> product = productEndpoint.getAll(pageable);
model.addAttribute("product", product.getContent());
model.addAttribute("page", product.getTotalPages());
model.addAttribute("pageNumber", product.getNumber());
return "index";
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String showById(@RequestParam(value = "id")Long id, Model model){
model.addAttribute("product", productEndpoint.TakeDetails(id));
return "details";
}
}
@Controller
public class DetailsController {
@Autowired
ProductEndpoint productEndpoint;
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public String showDetails(@PathVariable("id") Model model){
model.addAttribute("product");
return "details";
}
@RequestMapping(value = "/details/addtocart", method = RequestMethod.POST)
public String addToCart(){
return "redirect:/cart";
}
}
和index.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title></title>
<meta name="description" content="HTML 5 CSS 3 Spring Form"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<script src="../../js/libs/ajax.js" th:src="@{/js/libs/ajax.js}"></script>
</head>
<body>
<div id="container" align="center">
<form method="post">
<table border="1">
<tr>
<td>#</td>
<td></td>
<td align="center"><b>TITLE</b></td>
<td ><b>PRICE</b></td>
<td ></td>
<td></td>
</tr>
<tr th:each="sb, poz : ${product}">
<td th:text="${poz.count}">1</td>
<td ><img th:src="${sb.cover}" alt="cover"/> </td>
<td th:text="${sb.title}"></td>
<td th:text="${sb.price}"></td>
<td><a th:href="@{/details(id=${sb.id})}" >DETAILS</a></td>
<td ><b><a th:href="@{/cart(orderId=${sb.title})}" >ADD TO CART</a></b></td>
</tr>
</table>
</form>
<footer>
</footer>
</div>
</body>
</html>
和details.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title></title>
<meta name="description" content="HTML 5 CSS 3 Spring Form"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
</head>
<body >
<div id="container" align="center">
<header> </header>
<form method="get" th:object="${product}">
<table border="1">
<tr th:each="sb : ${product}">
<td><b>TITLE :</b></td>
<td th:text="${sb.title}"></td>
</tr>
<tr>
<td><b>AUTHORS :</b></td>
<td th:text="${sb.author}"></td>
</tr>
<tr>
<td><b>DESCRIPTION :</b></td>
<td th:text="${sb.description}">D
</td>
</tr>
<tr>
<td colspan="2" align="center">
</td>
</tr>
<tr>
<td><b>PRICE :</b></td>
<td th:text="${sb.price}">$ 39</td>
</tr>
</table>
</form>
<p>
<a th:href="@{/}">Return to home</a>
</p>
<footer>
</footer>
</div>
<!--! end of #container -->
</body>
</html>
感谢您的帮助。