这是我第一次问问题,所以有人来帮助我。我在编写程序时遇到了麻烦:
这是我的代码: productList.jsp
<%--
Document : product
Created on : Nov 18, 2018, 2:57:17 PM
Author : huynq
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="views/common/common.jsp"%>
<!DOCTYPE html>
<html>
<head>
<%@include file="views/common/head.jsp"%>
</head>
<body>
<jsp:include page="views/common/header.jsp">
<jsp:param name="curren" value="Product"/>
</jsp:include>
<div class="right col-sm-9">
<div class="bang">
<table class="table table-bordered table-hover">
<thead class="thead-dark">
<tr>
<th>ID Sản phẩm</th>
<th>ID Danh Mục</th>
<th>Mã Sản Phẩm</th>
<th>Tên Sản Phẩm</th>
<th>Giá Sản Phẩm</th>
<th>Ảnh Sản Phẩm</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<c:forEach var="listProduct" items="${listProduct}">
<tr>
<td>${listProduct.productId}</td>
<td>${listProduct.category}</td>
<td>${listProduct.sku}</td>
<td>${listProduct.productName}</td>
<td>${listProduct.price}</td>
<td><img src="${listProduct.src}" alt="" width="50" height="50"/></td>
<td>
<a href=" <c:url value="/ProductEditController?productId=${listProduct.productId}"/> " class="btn btn-warning btn-sm"><i class="far fa-edit"></i></a>
<a href=" <c:url value="/ProductDeleteController?productId=${listProduct.productId}"/> " class="btn btn-danger btn-sm"><i class="far fa-trash-alt"></i></a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<jsp:include page="views/common/footer.jsp">
<jsp:param name="curren" value="Product"/>
</jsp:include>
</body>
</html>
and this is **header.jsp**
<!-- begin snippet: js hide: false console: true babel: false -->
这是控制器: ProductListController.java
package controller;
import dao.ProductDAO;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Product;
/**
*
* @author huynq
*/
public class ProductListController extends HttpServlet {
ProductDAO productDAO;
public ProductListController(){
super();
}
@Override
public void init(ServletConfig config) throws ServletException {
this.productDAO = new ProductDAO();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Product> listProduct = this.productDAO.getListProduct();
System.out.println("TEST CONTROL: " + listProduct);
request.setAttribute("listProduct", listProduct);
RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/productList.jsp");
dispatcher.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// List<Product> listProduct = this.productDAO.getListProduct();
// System.out.println("TEST CONTROL: " + listProduct);
//
// request.setAttribute("listProduct", listProduct);
// RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/productList.jsp");
// dispatcher.forward(request, response);
}
}
这是模型: Product.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package model;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author huynq
*/
public class Product implements java.io.Serializable {
private int productId;
private Category category;
private String sku;
private String productName;
private int price;
private String src;
private String note;
private String noteDetail;
private String khuyenMai;
private Set carts = new HashSet(0);
public Product() {
}
public Product(int productId, String sku, String productName, int price) {
this.productId = productId;
this.sku = sku;
this.productName = productName;
this.price = price;
}
public Product(int productId, Category category, String sku, String productName, int price, String src, String note, String noteDetail, String khuyenMai, Set carts) {
this.productId = productId;
this.category = category;
this.sku = sku;
this.productName = productName;
this.price = price;
this.src = src;
this.note = note;
this.noteDetail = noteDetail;
this.khuyenMai = khuyenMai;
this.carts = carts;
}
//Get
public int getProductId() {
return productId;
}
public Category getCategory() {
return category;
}
public String getSku() {
return sku;
}
public String getProductName() {
return productName;
}
public int getPrice() {
return price;
}
public String getSrc() {
return src;
}
public String getNote() {
return note;
}
public String getNoteDetail() {
return noteDetail;
}
public String getKhuyenMai() {
return khuyenMai;
}
public Set getCarts() {
return carts;
}
//Set
public void setProductId(int productId) {
this.productId = productId;
}
public void setCategory(Category category) {
this.category = category;
}
public void setSku(String sku) {
this.sku = sku;
}
public void setProductName(String productName) {
this.productName = productName;
}
public void setPrice(int price) {
this.price = price;
}
public void setSrc(String src) {
this.src = src;
}
public void setNote(String note) {
this.note = note;
}
public void setNoteDetail(String noteDetail) {
this.noteDetail = noteDetail;
}
public void setKhuyenMai(String khuyenMai) {
this.khuyenMai = khuyenMai;
}
public void setCarts(Set carts) {
this.carts = carts;
}
}
这是DAO: ProductDAO.java
package dao;
import java.util.List;
import model.Product;
import org.hibernate.Session;
import org.hibernate.query.Query;
import util.HibernateUtil;
/**
*
* @author huynq
*/
public class ProductDAO {
Session session;
public List<Product> getListProduct() {
try {
this.openSession();
String sql = "from Product";
Query<Product> query = session.createQuery(sql);
List<Product> list = query.list();
System.out.println("TEST SP DAO LIST:" +list);
this.closeSession();
return list;
} catch (Exception e) {
System.out.println("CO LOI O DAY: "+e);
return null;
}
}
public Product getProduct(int productId) {
this.openSession();
Product product = this.session.find(Product.class, productId);
System.out.println("TEST FIND PRODUCT: " + product);
return product;
}
public void createProduct(Product product) {
try {
this.openSession();
this.session.beginTransaction();
this.session.save(product);
this.session.flush();
this.session.getTransaction().commit();
} catch (Exception e) {
this.session.getTransaction().rollback();
System.out.println("LOI O CREATE PRODUCT:" + e);
} finally {
this.openSession();
}
}
public void updateProduct(Product product) {
try {
this.openSession();
this.session.beginTransaction();
this.session.update(product);
this.session.flush();
this.session.getTransaction().commit();
} catch (Exception e) {
this.session.getTransaction().rollback();
System.out.println("LOI O UPDATE PRODUCT:" + e);
} finally {
this.openSession();
}
}
public void deleteProduct(int productId) {
try {
this.openSession();
Product category = this.session.find(Product.class, productId);
this.session.beginTransaction();
this.session.delete(category);
this.session.flush();
this.session.getTransaction().commit();
} catch (Exception e) {
System.out.println("LOI O DELETE PRODUCT: " + e);
this.session.getTransaction().rollback();
} finally {
this.closeSession();
}
}
//------------------------------------------------------------------------------
private void openSession() {
this.session = HibernateUtil.getSessionFactory().openSession();
}
private void closeSession() {
if (this.session != null && this.session.isOpen()) {
this.session.close();
}
}
}
这是 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--Phan Login-->
<servlet>
<servlet-name>LoginController</servlet-name>
<servlet-class>controller.LoginController</servlet-class>
</servlet>
<servlet>
<servlet-name>Logout</servlet-name>
<servlet-class>controller.Logout</servlet-class>
</servlet>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--Phan Admin-->
<servlet>
<servlet-name>AdminListController</servlet-name>
<servlet-class>controller.AdminListController</servlet-class>
</servlet>
<servlet>
<servlet-name>AdminEditController</servlet-name>
<servlet-class>controller.AdminEditController</servlet-class>
</servlet>
<servlet>
<servlet-name>AdminDeleteController</servlet-name>
<servlet-class>controller.AdminDeleteController</servlet-class>
</servlet>
<servlet>
<servlet-name>AdminCreateController</servlet-name>
<servlet-class>controller.AdminCreateController</servlet-class>
</servlet>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--Phan Account-->
<servlet>
<servlet-name>AccountListController</servlet-name>
<servlet-class>controller.AccountListController</servlet-class>
</servlet>
<servlet>
<servlet-name>AccountEditController</servlet-name>
<servlet-class>controller.AccountEditController</servlet-class>
</servlet>
<servlet>
<servlet-name>AccountDeleteController</servlet-name>
<servlet-class>controller.AccountDeleteController</servlet-class>
</servlet>
<servlet>
<servlet-name>AccountCreateController</servlet-name>
<servlet-class>controller.AccountCreateController</servlet-class>
</servlet>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<servlet>
<servlet-name>ProductListController</servlet-name>
<servlet-class>controller.ProductListController</servlet-class>
</servlet>
<servlet>
<servlet-name>ProductEditController</servlet-name>
<servlet-class>controller.ProductEditController</servlet-class>
</servlet>
<servlet>
<servlet-name>ProductCreateController</servlet-name>
<servlet-class>controller.ProductCreateController</servlet-class>
</servlet>
<servlet>
<servlet-name>ProductDeleteController</servlet-name>
<servlet-class>controller.ProductDeleteController</servlet-class>
</servlet>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/LoginController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Logout</servlet-name>
<url-pattern>/Logout</url-pattern>
</servlet-mapping>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--Phan Category-->
<servlet>
<servlet-name>CategoryListController</servlet-name>
<servlet-class>controller.CategoryListController</servlet-class>
</servlet>
<servlet>
<servlet-name>CategoryEditController</servlet-name>
<servlet-class>controller.CategoryEditController</servlet-class>
</servlet>
<servlet>
<servlet-name>CategoryCreateController</servlet-name>
<servlet-class>controller.CategoryCreateController</servlet-class>
</servlet>
<servlet>
<servlet-name>CategoryDeleteController</servlet-name>
<servlet-class>controller.CategoryDeleteController</servlet-class>
</servlet>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--Phan Admin-->
<servlet-mapping>
<servlet-name>AdminListController</servlet-name>
<url-pattern>/AdminListController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AdminEditController</servlet-name>
<url-pattern>/AdminEditController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AdminDeleteController</servlet-name>
<url-pattern>/AdminDeleteController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AdminCreateController</servlet-name>
<url-pattern>/AdminCreateController</url-pattern>
</servlet-mapping>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--Phan Account-->
<servlet-mapping>
<servlet-name>AccountListController</servlet-name>
<url-pattern>/AccountListController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AccountEditController</servlet-name>
<url-pattern>/AccountEditController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AccountDeleteController</servlet-name>
<url-pattern>/AccountDeleteController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AccountCreateController</servlet-name>
<url-pattern>/AccountCreateController</url-pattern>
</servlet-mapping>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--Phan Category-->
<servlet-mapping>
<servlet-name>CategoryListController</servlet-name>
<url-pattern>/CategoryListController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CategoryEditController</servlet-name>
<url-pattern>/CategoryEditController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CategoryCreateController</servlet-name>
<url-pattern>/CategoryCreateController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CategoryDeleteController</servlet-name>
<url-pattern>/CategoryDeleteController</url-pattern>
</servlet-mapping>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!--Phan Product-->
<servlet-mapping>
<servlet-name>ProductListController</servlet-name>
<url-pattern>/ProductListController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ProductEditController</servlet-name>
<url-pattern>/ProductEditController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ProductCreateController</servlet-name>
<url-pattern>/ProductCreateController</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ProductDeleteController</servlet-name>
<url-pattern>/ProductDeleteController</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
这是实用程序: HibernateUtil.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* @author huynq
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
getSessionFactory().close(); // Close caches and connection pools
}
}