1

I have already written the code for inserting my data into my DB but I'm a bit confused on how to retrieve that data in json format and print in my jsp view using a jQuery data table. I have written some code on retrieving but I'm still stuck. Please help.

Here are my code snippets:

entity class:

package model.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;

@Entity
public class Products {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int productId;
    @Size(min=1)
    private String productName;
    @Min(value=1, message = "Value cannot be zero or negative")
    private double unitPrice;
    @Size(min=1)
    private String productDescription;
    @Size(min=1)
    private String category;
    @Min(value = 1, message = " Quantity should be greater than zero and positive")
    @Max(value= 99, message = " Quantity limit exceeded, value should be less than 100")
    private int productQty;

    public Products() {}

    public Products(int productId, String productName, double unitPrice, String productDescription, String category, int productQty) {
        super();
        this.productQty = productQty;
        this.productId = productId;
        this.productName = productName;
        this.unitPrice = unitPrice;
        this.productDescription = productDescription;
        this.category = category;
    }

    public int getProductQty() {
        return productQty;
    }

    public void setProductQty(int productQty) {
        this.productQty = productQty;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

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

    public double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

DAOImplementation class:

    package model.daoimpl;

    import java.util.List;

    import org.hibernate.Session;

    import model.config.HibernateUtil;
    import model.dao.IProductsDAO;
    import model.entity.Products;

    public class ProductsDAOImpl implements IProductsDAO {
        private Session sess;
        public ProductsDAOImpl() {
            sess = HibernateUtil.getSessionFactory().openSession();
        }
        public boolean insertProduct(Products p) {
    boolean b = true;

            try
            {
                sess.beginTransaction();
                sess.save(p);
                sess.getTransaction().commit();

            }catch(Exception ex)
            {
                b = false;
                sess.getTransaction().rollback();
                ex.printStackTrace();
            }

            return b;
        }

        public List<Products> getProducts()
        {
            List<Products> lp = null;
            try
            {
                sess.beginTransaction();
                lp = sess.createQuery("from Product",Products.class).getResultList();       
            }catch(Exception ex)
            {
                ex.printStackTrace();
            }
            return lp;
        }   

        }

controller class:

@Controller
public class ProductController {
    @RequestMapping(value="/manageproducts", method= RequestMethod.GET)
    public String manageProductPage() {
        return "manageproducts";
    }

    @RequestMapping(value="/insertproducts",method = RequestMethod.POST)
    public String addInserProductsPage(@ModelAttribute("Products")Products p) {
        IProductsDAO ip = new ProductsDAOImpl();
        boolean b = ip.insertProduct(p);
        if(b)
            return "success";
        else
            return "manageproducts";
    }

    @RequestMapping(value="/listproducts", method = RequestMethod.GET)
    public List<Products> listAllProducts(){
        IProductsDAO ip = new ProductsDAOImpl();
        return ip.getProducts();
    }

}

So as you can see I have created the getproducts function but I haven't created the view for displaying products for which I want to use the jQuery datatable, Also I'm a bit confused on how to map the view with my controller. So please help.

1 Answer 1

3

You can use Model to send your list to the view page

    @RequestMapping(value="/listproducts",      method = RequestMethod.GET) 
    public String listAllProducts(Model model){
        IProductsDAO ip = new ProductsDAOImpl(); 
        List<Products> products = ip.getProducts(); 
        model.addAttribute ("products",products);
        Return "your view name without .jsp";
      }
Sign up to request clarification or add additional context in comments.

6 Comments

One doubt that I have is in my jsp how will my table get reffrenced to the controller ?
The name of the attribute is the name of tour list in jsp in this case your list is called products , you can loop through it and display the products
something like this ? '<c:forEach items="${products}" var="products">'
Yes that's correct and you can access values using ${products.productsName} for example if you want the name of the product
Ok, Thankyou very very much
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.