简体   繁体   English

Spring MVC Hibernate Web应用程序

[英]Spring MVC Hibernate web application

Spring MVC + Hibernate web application with mysql database. Spring MVC +带有mysql数据库的Hibernate Web应用程序。 3 tables (products, members and cart to connect this two). 3个表(将这两个表连接在一起的产品,成员和购物车)。 Members table have two different users: admin and customer. 成员表具有两个不同的用户:管理员和客户。 It should be something like Online Store. 它应该像在线商店。 To have Administrator and Customer users. 具有管理员和客户用户。 Admin entering new, edit and delete products. 管理员输入新产品,编辑产品和删除产品。 Customer to list all products and add to cart part. 客户列出所有产品并添加到购物车零件。 But before that it should have log in and sign up part. 但在此之前,它应该已经登录并注册了一部分。 So Admin or Customer can log in or sign up. 因此,管理员或客户可以登录或注册。 So I have welcome page, index.jsp, from which I need links to: 因此,我有一个欢迎页面,index.jsp,我需要从该页面链接到:

all_products.jsp -> list all products from products table mysql database all_products.jsp->列出产品表mysql数据库中的所有产品

signup.jsp -> Add new member form and sending data to members table mysql database. signup.jsp->添加新的成员表单并将数据发送到成员表mysql数据库。

login.jsp -> Log In form login.jsp->登录表单

From login.jsp depending who logged in: 从login.jsp(取决于谁登录):

Customer -> index.jsp 客户-> index.jsp

Admin -> admin.jsp 管理员-> admin.jsp

admin.jsp -> add.jsp, all_products.jsp with edit and delete option. admin.jsp-> add.jsp,all_products.jsp,带有“编辑和删除”选项。

add.jsp-> add new product form add.jsp->添加新产品表格

I setup up a new project in Netbeans, with Spring mvc and hibernate framework, connect with mysql database, set up server, glassfish... etc... 我在Netbeans中使用Spring mvc和hibernate框架设置了一个新项目,与mysql数据库连接,设置了服务器,glassfish ...等。

Then add... 然后加...

Members.java 成员.java

package model;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name="members")
public class Members  implements java.io.Serializable {

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO) 
 private int memberId;
 private String userName;
 private String password;
 private String fullName;
 private String email;
 private String address;
 private String gender;
 private String dateOfBirth;
 private String memberType;
 private Set<Cart> carts = new HashSet<Cart>(0);

public Members() {
}

public Members(int memberId) {
    this.memberId = memberId;
}
public Members(int memberId, String userName, String password, String fullName, String email, String address, String gender, String dateOfBirth, String memberType, Set<Cart> carts) {
   this.memberId = memberId;
   this.userName = userName;
   this.password = password;
   this.fullName = fullName;
   this.email = email;
   this.address = address;
   this.gender = gender;
   this.dateOfBirth = dateOfBirth;
   this.memberType = memberType;
   this.carts = carts;
}

public int getMemberId() {
    return this.memberId;
}

public void setMemberId(int memberId) {
    this.memberId = memberId;
}
public String getUserName() {
    return this.userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}
public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}
public String getFullName() {
    return this.fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}
public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}
public String getAddress() {
    return this.address;
}

public void setAddress(String address) {
    this.address = address;
}
public String getGender() {
    return this.gender;
}

public void setGender(String gender) {
    this.gender = gender;
}
public String getDateOfBirth() {
    return this.dateOfBirth;
}

public void setDateOfBirth(String dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}
public String getMemberType() {
    return this.memberType;
}

public void setMemberType(String memberType) {
    this.memberType = memberType;
}
public Set<Cart> getCarts() {
    return this.carts;
}

public void setCarts(Set<Cart> carts) {
    this.carts = carts;
 }
}

Products.java 产品.java

package model;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name="products")
public class Products  implements java.io.Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO) 
private int productId;
private String productName;
private String productType;
private String description;
private Double price;
private String productColor;
private String productBrand;
private String productSize;
private Integer allProductsQuantity;
private Set<Cart> carts = new HashSet<Cart>(0);

public Products() {
}

public Products(int productId) {
    this.productId = productId;
}
public Products(int productId, String productName, String productType, String      description, Double price, String productColor, String productBrand, String productSize,  Integer allProductsQuantity, Set<Cart> carts) {
   this.productId = productId;
   this.productName = productName;
   this.productType = productType;
   this.description = description;
   this.price = price;
   this.productColor = productColor;
   this.productBrand = productBrand;
   this.productSize = productSize;
   this.allProductsQuantity = allProductsQuantity;
   this.carts = carts;
}

public int getProductId() {
    return this.productId;
}

public void setProductId(int productId) {
    this.productId = productId;
}
public String getProductName() {
    return this.productName;
}

public void setProductName(String productName) {
    this.productName = productName;
}
public String getProductType() {
    return this.productType;
}

public void setProductType(String productType) {
    this.productType = productType;
}
public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}
public Double getPrice() {
    return this.price;
}

public void setPrice(Double price) {
    this.price = price;
}
public String getProductColor() {
    return this.productColor;
}

public void setProductColor(String productColor) {
    this.productColor = productColor;
}
public String getProductBrand() {
    return this.productBrand;
}

public void setProductBrand(String productBrand) {
    this.productBrand = productBrand;
}
public String getProductSize() {
    return this.productSize;
}

public void setProductSize(String productSize) {
    this.productSize = productSize;
}
public Integer getAllProductsQuantity() {
    return this.allProductsQuantity;
}

public void setAllProductsQuantity(Integer allProductsQuantity) {
    this.allProductsQuantity = allProductsQuantity;
}
public Set<Cart> getCarts() {
    return this.carts;
}

public void setCarts(Set<Cart> carts) {
    this.carts = carts;
 }
}

Cart.java 购物车

package model;

public class Cart  implements java.io.Serializable {


 private int cartId;
 private Members members;
 private Products products;
 private Integer cartQuantity;

 public Cart() {
 }


public Cart(int cartId) {
    this.cartId = cartId;
}
public Cart(int cartId, Members members, Products products, Integer cartQuantity) {
   this.cartId = cartId;
   this.members = members;
   this.products = products;
   this.cartQuantity = cartQuantity;
}

public int getCartId() {
    return this.cartId;
}

public void setCartId(int cartId) {
    this.cartId = cartId;
}
public Members getMembers() {
    return this.members;
}

public void setMembers(Members members) {
    this.members = members;
}
public Products getProducts() {
    return this.products;
}

public void setProducts(Products products) {
    this.products = products;
}
public Integer getCartQuantity() {
    return this.cartQuantity;
}

public void setCartQuantity(Integer cartQuantity) {
    this.cartQuantity = cartQuantity;
}

}

I add service also for this classes. 我也为此类添加服务。

MemberService 会员服务

package service;

import java.util.List;
import model.Members;

public interface MembersService {
    public void add(Members members);
    public void edit(Members members);
    public void delete(int memberId);
    public Members getMembers(int memverId);
    public List getAllMembers();
}

ProductsService 产品服务

package service; 包装服务;

import java.util.List;
import model.Products;

public interface ProductsService {
    public void add(Products products);
    public void edit(Products products);
    public void delete(int productId);
    public Products getProducts(int productId);
    public List getAllProducts();
}

Also add ServiceImpl 同时添加ServiceImpl

MembersServiceImpl MembersServiceImpl

package service;

import DAO.MembersDAO;
import java.util.List;
import javax.jms.Session;
import javax.transaction.Transactional;
import model.Members;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Service
public class MembersServiceImpl implements MembersService{

@Autowired
private MembersDAO membersDAO;

@Transactional
public void add(Members members) {
    membersDAO.add(members);
}

@Transactional
public void edit(Members members) {
    membersDAO.edit(members);
}

@Transactional
public void delete(int productId) {
    membersDAO.delete(productId);
}

@Transactional
public Members getMembers(int productId) {
    return membersDAO.getMembers(productId);
}

@Transactional
public List getAllMembers() {
    return membersDAO.getAllMembers();
}

}

ProductsServiceImpl 产品服务Impl

package service;

import DAO.ProductsDAO;
import java.util.List;
import javax.jms.Session;
import javax.transaction.Transactional;
import model.Products;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Service
public class ProductsServiceImpl implements ProductsService{

@Autowired
private ProductsDAO productsDAO;

@Transactional
public void add(Products products) {
    productsDAO.add(products);
}

@Transactional
public void edit(Products products) {
    productsDAO.edit(products);
}

@Transactional
public void delete(int productId) {
    productsDAO.delete(productId);
}

@Transactional
public Products getProducts(int productId) {
    return productsDAO.getProducts(productId);
}

@Transactional
public List getAllProducts() {
    return productsDAO.getAllProducts();
}

}

And ProductsContoller 和产品Contoller

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Products;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.ProductsService;

public class ProductsController extends SimpleFormController {

public ProductsController() {

    setCommandClass(Products.class);
    setCommandName("products");
    setSuccessView("products");
    setFormView("products");
}

 protected ModelAndView products(HttpServletRequest request, HttpServletResponse   response, Object command, BindException errors) throws Exception {
    Products products = (Products) command;
    ModelAndView mv = new ModelAndView(getSuccessView());
    /*ModelAndView addObject = mv.addObject("products",     ProductsService.ProductsService(products.getValue()));*/
    return mv;
}
    @Autowired
    private ProductsService productsService;
    public void setProductsService(ProductsService productsService) {
        this.productsService = productsService;
}

} }

Here I am stack. 我在这里。

I also have simple MembersDAO.java, ProductsDAO.java and there implementations. 我也有简单的MembersDAO.java,ProductsDAO.java及其实现。

...and fist question is how to connect two .jsp pages? ...而第一个问题是如何连接两个.jsp页面? How to make simple navigation bar to connect first all my .jsp(view) pages? 如何使简单的导航栏首先连接我的所有.jsp(view)页面? To make simple nav bar on header and use on all my pages. 要在标题上制作简单的导航栏,并在我的所有页面上使用。 I know i should use spring contorollers... 我知道我应该使用弹簧控制器。

How to make simple controller that will take me to all_products.jsp from index.jsp and list all products from products table from mysql database??? 如何做一个简单的控制器,它将把我带到index.jsp的all_products.jsp并列出mysql数据库中products表中的所有产品?

How to import spring security, log in section in my app? 如何导入Spring Security,在我的应用程序中登录部分? Also add new product form... 还要添加新的产品表格...

My app is working and deplopying... I can upload web.xml and servlet.xml but did change things... 我的应用正在运行并且正在部署...我可以上载web.xml和servlet.xml,但是确实有所改变...

Can anyone help me?! 谁能帮我?! Thank you very much. 非常感谢你。

in your .jsp. 在您的.jsp中。 create your navigation. 创建您的导航。 If products on href doesn't work. 如果href上的产品不起作用。 Then add 然后加

${pageContext.request.contextPath}/products 

this will be your navigation 这将是您的导航

<ul>
    <li>
        <a href="${pageContext.request.contextPath}">Home</a>
    </li>
    <li>
        <a href="products">Products</a>
    </li>
    <li>
        <a href="users">Users</a>
    </li>
</ul>

navigation should look like this or depends on what you want 导航应该看起来像这样或取决于您想要什么

  • Home
  • Products 产品展示
  • Users 用户数

and the simple controller for the views or for your .jsp files. 以及用于视图或.jsp文件的简单控制器。

 @RequestMapping(value = "/home", method = RequestMethod.GET)
            public ModelAndView home() {        
                return new ModelAndView("home"); //home.jsp
            }

@RequestMapping(value = "/products", method = RequestMethod.GET)
    public ModelAndView products() {        
        return new ModelAndView("products");
    }

 @RequestMapping(value = "/users", method = RequestMethod.GET)
        public ModelAndView users() {       
            return new ModelAndView("users");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM