Skip to main content

not see the image name and the price in the shopping cart in its place is the id

No replies
matf500
Offline
Joined: 2013-08-17
Points: 0

Not see the image name and the price in the shopping cart in its place is the id
I'm working with struts2 here I show the codes:

1-. package roseindia.action.method.cart;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.Double.*;
import javax.lang.model.type.ReferenceType;

import org.apache.catalina.Session;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import roseindia.model.Cart;
import roseindia.action.product.*;
import dao.domain.CategoryDao;
import dao.domain.ProductDao;
import dao.tables.Product;

public class AddToCartAction extends ActionSupport implements ServletRequestAware{

/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private Integer productid;
private String productname;
private Double productprice;
private String description;
private String userImage;
private Cart cart;
private Product prod;

public String execute() throws Exception
{
HttpSession session = request.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if( cart == null)
{
cart = new Cart();
}
//An to proion iparhei ksana tote epistrefoume
if( cart.selectProductByProductid(Integer.parseInt(request.getParameter("id"))) != null )
{
return SUCCESS;
}
//prosthese to proion sto kalathi
Product prod = new Product();
prod.setProductid(Integer.parseInt(request.getParameter("id")));
prod.setUserImage(request.getParameter("id"));
prod.setProductname(request.getParameter("id"));
prod.setDescription(request.getParameter("id"));
prod.setProductprice(Double.parseDouble(request.getParameter("id")));
prod.setQuantity(1);
cart.addToCart(prod);
session.setAttribute("cart", cart);
return SUCCESS;
}

public Product getProduct()
{
return prod;
}

public void setProductname(String productname)
{
this.productname = productname;
}

public void setProductprice(Double productprice)
{
this.productprice = productprice;
}

public void setDescription(String description)
{
this.description = description;
}

public void setUserImage(String userImage)
{
this.userImage = userImage;
}

//@Override
//public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub

public void setServletRequest(HttpServletRequest request)
{
this.request = request;
}

}

2-. package roseindia.model;

import dao.domain.CategoryDao;
import dao.domain.ProductDao;
import dao.tables.Product;
import roseindia.action.product.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Cart {

List items;
private double total_productprice;

public Cart() {
items = new ArrayList();
this.total_productprice = 0;
}

public void addToCart(Product prod) {
this.items.add(prod);
this.total_productprice = this.total_productprice + prod.getProductprice()
* prod.getQuantity();
}

public void removeFromCart(Product prod) {

this.items.remove(prod);
this.total_productprice = this.total_productprice - prod.getProductprice();
if (this.items.size() == 0) {
this.total_productprice = 0;
}
}

public void calculate_total()
{
Product current;
this.total_productprice = 0;
for(Iterator prod = items.iterator(); prod.hasNext();)
{
current = prod.next();
this.total_productprice = this.total_productprice + current.getQuantity() * current.getProductprice();
}
}

public List getItems() {
return items;
}

public void setItems(List items) {
this.items = items;
}

public double getTotal_productprice() {
return total_productprice;
}

public void setTotal_productprice(double totalProductprice) {
this.total_productprice = totalProductprice;
}
//Epistrefei ton arithmo twn monadikwn stoiheiwn poy yparxoun sto kalathi
public int getnumOfItems() {
return this.getItems().size();
}

//Epilegei to proion me vasi to id
public Product selectProductByProductid(int productid) {
Product prod = null;
for (Iterator it = items.iterator(); it.hasNext();) {
prod = it.next();
if (prod.getProductid() == productid) {
return prod;
}
}
return null;
}

}

3-. <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@page import="dao.tables.Product"%>
<%@page import="roseindia.model.Cart"%>
<%@page import="dao.domain.UserDao"%>
<%@page import="dao.tables.User"%>

<script src="./JQuery/jquery-1.4.2.js" type="text/javascript"></script>

<%@page import="dao.domain.ProductDao"%>

" rel="stylesheet"
type="text/css" />



Your shopping Cart



Shopping Cart: <%
//An to kalathi einai adeio tote tipose "Your Cart is empty"
if (((Cart) session.getAttribute("cart")).getItems().size() == 0) {
%> Your Cart is empty

<%
return;
}
%>

Imagen Productname Price Quantity





Total




action="viewProductUser">


4-. package dao.tables;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table (name = "product")
public class Product implements Serializable {
@Id
@GeneratedValue

/** identifier field */
private Integer productid;

/** persistent field */
private String categoryname;

/** persistent field */
private String productname;

/** persistent field */
private Double productprice;

/** persistent field */
private String listprice;

/** persistent field */
private String userImage;

/** persistent field */
private Integer quantity;

/** persistent field */
private String description;

/** nullable persistent field */
private String briefdisc;

/** default constructor */
public Product() {
}

/** full constructor */
public Product(Integer productid, String categoryname, String productname, Double productprice, String listprice, String imagename, Integer quantity, String description, String briefdisc) {
this.productid = productid;
this.categoryname = categoryname;
this.productname = productname;
this.productprice = productprice;
this.listprice = listprice;
this.userImage = imagename;
this.quantity = quantity;
this.description = description;
this.briefdisc = briefdisc;
}

@Column(name = "productid")
public Integer getProductid() {
return this.productid;
}

public void setProductid(Integer productid) {
this.productid = productid;
}

@Column(name = "categoryname")
public String getCategoryname() {
return categoryname;
}

public void setCategoryname(String categoryname) {
this.categoryname = categoryname;
}

@Column(name = "productname")
public String getProductname() {
return this.productname;
}

public void setProductname(String productname) {
this.productname = productname;
}

@Column(name = "productprice")
public Double getProductprice() {
return this.productprice;
}

public void setProductprice(Double productprice) {
this.productprice = productprice;
}

@Column(name = "listprice")
public String getListprice() {
return this.listprice;
}

public void setListprice(String listprice) {
this.listprice = listprice;
}

@Column(name = "userImage")
public String getUserImage() {
return userImage;
}

public void setUserImage(String userImage) {
this.userImage = userImage;
}

// public String getImagename() {
// return this.imagename;
// }
//
// public void setImagename(String imagename) {
// this.imagename = imagename;
// }
@Column(name = "quantity")
public Integer getQuantity() {
return this.quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}

@Column(name = "description")
public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

@Column(name = "briefdisc")
public String getBriefdisc() {
return this.briefdisc;
}
public void setBriefdisc(String briefdisc) {
this.briefdisc = briefdisc;
}
}

5-.package dao.domain;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import dao.HibernateUtil.HibernateUtil;
import dao.tables.Product;

public class ProductDao {

HibernateUtil util = new HibernateUtil();
Product productDatail=new Product();

public List showAll() {

Session session = util.getSessionfactory().openSession();
Transaction transaction = null;
List products = new ArrayList();
try {
transaction = session.beginTransaction();
Query query = session.createQuery("from Product");
products = query.list();
transaction.commit();
} catch (HibernateException e) {
System.out.println("Inside Catch");
e.getMessage();
e.toString();
transaction.rollback();
} finally {
session.close();
}
return products;
}

public List search(int productId) {

Session session = util.getSessionfactory().openSession();
Transaction transaction = null;
List products = new ArrayList();

try {
System.out.println("Inside try");
transaction = session.beginTransaction();
Query query = session
.createQuery("from Product where productid = :productid");
query.setParameter("productid", productId);
products = query.list();

for (Iterator iterator = products.iterator(); iterator.hasNext();) {
Product productDatail = (Product) iterator.next();
System.out.println(productDatail.getProductname());
}

transaction.commit();
} catch (Exception e) {
System.out.println("Inside Catch");
e.printStackTrace();
transaction.rollback();
} finally {
session.close();
}
return products;
}

public void addProduct(Product product) {
Session session = util.getSessionfactory().openSession();
session.save(product);
}

public void deleteProduct(int id){
Session session = util.getSessionfactory().openSession();
Transaction transaction = null;

try {
transaction = session.beginTransaction();
Query query = session.createQuery("delete Product where productid = :productid");
query.setParameter("productid", id);
int result=query.executeUpdate();
transaction.commit();

} catch (Exception e) {
System.out.println("Inside Catch");
e.getMessage();
e.toString();
transaction.rollback();
} finally {
session.close();
}
}

public List SearchbyName(String name) {

Session session = util.getSessionfactory().openSession();
Transaction transaction = null;
List products = new ArrayList();

try {

transaction = session.beginTransaction();
Query query = session
.createQuery("from Product where productname like :productname");
query.setParameter("productname", name + "%");
products = query.list();

for (Iterator iterator = products.iterator(); iterator.hasNext();) {
Product productDatail = (Product) iterator.next();
System.out.println(productDatail.getDescription());
}

transaction.commit();
} catch (Exception e) {

e.getMessage();
e.toString();
transaction.rollback();
} finally {
session.close();
}
return products;
}

//Search by category

public List searchByCategory(String category) {
Session session = util.getSessionfactory().openSession();
Transaction transaction = null;
List products = new ArrayList();
try {
transaction = session.beginTransaction();
Query query = session
.createQuery("from Product where categoryname = :categoryname");
query.setParameter("categoryname", category);
products = query.list();
transaction.commit();
} catch (Exception e) {
e.getMessage();
e.toString();
transaction.rollback();
} finally {
session.close();
}
return products;
}

public Product searchProduct(int productId) {

Session session = util.getSessionfactory().openSession();
Transaction transaction = null;
List products = new ArrayList();
Product productDatail = null;

try {
System.out.println("Inside try");
transaction = session.beginTransaction();
Query query = session
.createQuery("from Product where productid = :productid");
query.setParameter("productid", productId);
products = query.list();

for (Iterator iterator = products.iterator(); iterator.hasNext();) {
productDatail = (Product) iterator.next();
}

transaction.commit();
} catch (Exception e) {
System.out.println("Inside Catch");
e.printStackTrace();
transaction.rollback();
} finally {
session.close();
}
return productDatail;
}

public void updateProduct(Product product){
Session session = util.getSessionfactory().openSession();
try{
Query query=session.createQuery("update Product set categoryname = :categoryname, productname = :productname, productprice = :productprice, listprice = :listprice, userImage = :userImage, quantity = :quantity, description = :description, briefdisc = :briefdisc where productid = :productid");
query.setParameter("categoryname", product.getCategoryname());
query.setParameter("productname", product.getProductname());
query.setParameter("productprice", product.getProductprice());
query.setParameter("listprice", product.getListprice());
query.setParameter("userImage", product.getUserImage());
query.setParameter("quantity", product.getQuantity());
query.setParameter("description", product.getDescription());
query.setParameter("briefdisc", product.getBriefdisc());
query.setParameter("productid", product.getProductid());
query.executeUpdate();
}
catch(HibernateException e){
e.printStackTrace();
}
finally{
session.close();
}
}

public void updateProductWithoutImage(Product product){
Session session = util.getSessionfactory().openSession();

try{
Query query=session.createQuery("update Product set categoryname = :categoryname, productname = :productname, productprice = :productprice, listprice = :listprice, quantity = :quantity, description = :description, briefdisc = :briefdisc where productid = :productid");
query.setParameter("categoryname", product.getCategoryname());
query.setParameter("productname", product.getProductname());
query.setParameter("productprice", product.getProductprice());
query.setParameter("listprice", product.getListprice());
query.setParameter("quantity", product.getQuantity());
query.setParameter("description", product.getDescription());
query.setParameter("briefdisc", product.getBriefdisc());
query.setParameter("productid", product.getProductid());
query.executeUpdate();
}
catch(HibernateException e){
e.printStackTrace();
}
finally{
session.close();
}
}

public Product viewAll() {

Session session = util.getSessionfactory().openSession();
Transaction transaction = null;
List products = new ArrayList();
try {
transaction = session.beginTransaction();
Query query = session.createQuery("from Product");
products = query.list();
for (Iterator iterator = products.iterator(); iterator.hasNext();) {
productDatail = (Product) iterator.next();
}
transaction.commit();
} catch (HibernateException e) {
System.out.println("Inside Catch");
e.getMessage();
e.toString();
transaction.rollback();
} finally {
session.close();
}
return productDatail;
}

}

6-. <?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">




/resources/login.jsp


/resources/home.jsp
/resources/login.jsp


/resources/home.jsp


cartView.jsp


cartView.jsp


/resources/logoutPage.jsp


/resources/registerUser.jsp


/resources/home.jsp
/resources/registerUser.jsp


cartView.jsp
cartView.jsp


checkout.jsp


/success.jsp
/checkout.jsp


resources/viewProduct.jsp


resources/viewProductDetail.jsp

class="roseindia.action.product.SearchProduct">
resources/viewProduct.jsp

class="roseindia.action.product.SearchProduct">
resources/viewProduct.jsp

class="roseindia.action.product.SearchProduct">
resources/viewProduct.jsp

class="roseindia.action.product.SearchProduct">
resources/viewProduct.jsp


resources/shippingOptions.jsp


resources/myCart.jsp


resources/myCart.jsp


resources/purchaseReport.jsp


resources/logoutPage.jsp