程序正在执行选择查询也在控制台中运行但未在页面上显示,请检查程序并更正我谢谢
输出
package com.ecom.Model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
@Table(name="product")
public class Product {
@Id
@Column
private int id;
@Column
@NotNull
@Size(min=1,message="is required")
private String product_name;
@Column
@NotNull
@Size(min=1,message="is required")
private String manufacturer;
@Column
private int stock;
@Column
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Product(int id, String product_name, String manufacturer, int stock, String description) {
super();
this.id = id;
this.product_name = product_name;
this.manufacturer = manufacturer;
this.stock = stock;
this.description = description;
}
public Product()
{}
@Override
public String toString() {
return "Product [id=" + id + ", product_name=" + product_name + ", manufacturer=" + manufacturer + ", stock="
+ stock + ", description=" + description + "]";
}
}
产品控制器类 在控制器中,我创建了模型,在产品参考调用getAllProduct方法的帮助下将数据保存到模型中。
package com.ecom.Controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ecom.DAO.ProductDAO;
import com.ecom.Model.Product;
@Controller
public class ProductController {
//injecting Dependecy
@Autowired
private ProductDAO productDAO;
@RequestMapping("/ProductList")
public String ListProducts(Model theModel)
{
List<Product> theProducts = productDAO.getAllProduct();
theModel.addAttribute("products",theProducts);
return "productlist";
}
@RequestMapping("/ProductForm")
public String ProductForm(Model theModel)
{
Product theProduct = new Product();
theModel.addAttribute("addProduct", theProduct);
return "productform";
}
@RequestMapping("/saveProduct")
public String saveProduct(@ModelAttribute("addProduct") Product theProdut)
{
productDAO.addProducts(theProdut);
return "productform";
}
}
产品Dao实施
package com.ecom.DAOImplementation;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.ecom.DAO.ProductDAO;
import com.ecom.Model.Product;
@Repository("ProductDAO")
public class ProductImpl implements ProductDAO {
@Autowired
private SessionFactory sessionFactory;
@Transactional
public List<Product> getAllProduct() {
Session currentSession = sessionFactory.getCurrentSession();
Query<Product> theQuery = currentSession.createQuery("from Product",Product.class);
List<Product> products = theQuery.getResultList();
return products;
}
@javax.transaction.Transactional
public void addProducts(Product theProdut)
{
Session currentSession = sessionFactory.getCurrentSession();
System.out.println("Adding Product");
currentSession.save(theProdut);
System.out.println("Success");
}
}
Jsp page
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Manufacturer</th>
<th>Stock</th>
<th>Description</th>
</tr>
<c:forEach var="tempCustomer" items="${products}">
<tr>
<td> ${tempCustomer.id} </td>
<td> ${tempCustomer.product_name} </td>
<td> ${tempCustomer.manufacturer} </td>
<td> ${tempCustomer.stock} </td>
<td> ${tempCustomer.description} </td>
</tr>
</c:forEach>
</table>
</body>
</html>
答案 0 :(得分:0)
试着通过创建如下所示的样本列表来设置它。
@RequestMapping("/ProductList")
public String ListProducts(Model theModel)
{
List<Product> theProducts = productDAO.getAllProduct();
Product product = new Product(1,"pro-1","man-1",10,"pro-1 desc");
theProducts.add(product);
product = new Product(2,"pro-2","man-2",10,"pro-2 desc");
theProducts.add(product);
product = new Product(3,"pro-3","man-3",10,"pro-3 desc");
theProducts.add(product);
product = new Product(4,"pro-4","man-4",10,"pro-4 desc");
theProducts.add(product);
product = new Product(5,"pro-5","man-5",10,"pro-5 desc");
theProducts.add(product);
theModel.addAttribute("products",theProducts);
return "productlist";
}
JSP页面productlist.jsp
是as is
。没有做任何改变。我得到如下输出。
您可以尝试使用调试语句来查看theProducts
是否已正确填充。