LoginSignup
0
0

More than 1 year has passed since last update.

カート動いた

Posted at
1 / 7

取り急ぎ動いたので送っておきます。

1,フィールド追加
Item.java(Beanbs)

2,新規作成ファイル
Cartitem.Java(Beanbs)

3,Post記述追加
CartServlet.Java

4,新規作成ファイル
cartLogic.java

5,新規作成ファイル
CartDAO.Java

6新規作成ファイル
cartCheck.jsp

フィールド追加
Item.java(Beanbs)

package model;

public class Item {
private String category;
private String name;
private String explanation;
private String image_path;
private String image_path2;
private String image_path3;
private int price;
private int quantity;
private int cart_quantity;//追加*カートに入っている個数を取ってくるのに必要でした。


新規作成ファイル*Listはあまり機能してない感じです。CartServletでもっとうまく使えそうなきがします。
Cartitem.Java(Beanbs)

package model;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

public class CartItem implements Serializable {

private List<Item> cart2Item;
private LinkedHashMap<String, Integer> cartMap;


public CartItem () {}


public List<Item> getCart2Item() {
	return cart2Item;
}
public void setCart2Item(List<Item> cart2Item) {
	this.cart2Item = cart2Item;
}

public LinkedHashMap<String, Integer> getCartMap() {
	return cartMap;
}

public void setCartMap(LinkedHashMap<String, Integer> cart) {
	this.cartMap = cart;
}

}


Post記述追加
CartServlet.Java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
System.out.println("CartServlet内post");
HttpSession session =request.getSession();
LinkedHashMap cart = (LinkedHashMap) session.getAttribute("cart");

	System.out.println("cart"+cart);
	CartItem cartItems = new CartItem();
	
	//CartItemBeansのMapにセッションで持ってきたカート情報を代入
	cartItems.setCartMap(cart);
	CartLogic c = new CartLogic();
	List<Item> cartList = c.execute(cartItems);
	System.out.println("cartItem55"+ cartItems.getCart2Item());

	session.setAttribute("cartList", cartList);
	session.setAttribute("cartItems", cartItems);//とりあえずSessoinに入れて見ましたが取り出せないです。
	System.out.println("ccartItems"+ cartItems);
	System.out.println("cartItems.getCart2Item()"+ cartItems.getCart2Item());
	RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/cart/cartCheck.jsp");
	dispatcher.forward(request, response);
}

新規作成ファイル※無理やり動いた感じでしっくりきていませんw
cartLogic.java

package model;

import java.util.List;

import dao.CartDAO;

public class CartLogic {

public List<Item> execute(CartItem Jbk) {
	System.out.println(" CartLogic");
CartDAO dao = new CartDAO();
Jbk.setCart2Item(dao.findCart(Jbk));
return Jbk.getCart2Item();
}

}


新規作成ファイル
CartDAO.Java

public class CartDAO {
private final String JDBC_URL="jdbc:mysql://localhost:3306/haynth?characterEncoding=UTF-8&serverTimezone=JST";
private final String DB_USER="root";
private final String DB_PASS="root";

public List<Item> findCart(CartItem Jbk) {
	List<Item> cartList = new ArrayList<>();
	System.out.println("cartList_new ArrayList<>()");
	
	try {
		
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e1) {
		// TODO: handle exception
		e1.printStackTrace();
	}

//データベースへ接続
try (Connection conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS)) {
System.out.println("CartDAOスタート");
//ItemCategoryServleのkeyを持ってきたい
for (String cart_item : Jbk.getCartMap().keySet()) {
int cart_item_quant = Jbk.getCartMap().get(cart_item);

		String sql = null;
		sql = "SELECT * FROM `item` WHERE `ITEM_ID` = " + cart_item+";";
		PreparedStatement pSmt = conn.prepareStatement(sql);
		//SELECTを実行して結果表を取得
		ResultSet rs = pSmt.executeQuery();
		
		//結果表に格納されたレコードの内容をインスタンスに設定し、ArrayListインスタンスに追加
		while (rs.next()) {
			
			System.out.println("while (rs.next())内");
			String name = rs.getString("NAME");
			int price= rs.getInt("PRICE");
			int cart_quant = cart_item_quant;
			int stock_quantity = rs.getInt("QUANTITY");
			String image_path = rs.getString("IMAGE_PATH");
			int item_id = rs.getInt("ITEM_ID");
			System.out.println(image_path);
			
	
			Item item = new Item( name,image_path,price, stock_quantity,cart_quant,item_id);
			 System.out.println(item);
			cartList.add(item);
			
			System.out.println("cartList"+cartList);
			System.out.println("Javabeans"+Jbk.getCart2Item());
		}
		}
	
	}catch (SQLException e) {
		e.printStackTrace();
		return null;
	}
	System.out.println(Jbk.getCart2Item());
	return cartList;
}

}


新規作成ファイル
cartCheck.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="model.CartItem,model.Item,java.util.List" %>
<%
//
List cartList = (List) session.getAttribute("cartList");

%>

HAYNTH

Cart

<% int littletotal = 0; %> <% int alltotal = 0; %> <% for(Item c : cartList) {%>

<% littletotal = c.getPrice()*c.getCart_quantity(); %>
<% alltotal += littletotal; %>

Koushin

<%= c.getName() %>:<%= c.getPrice() %>円

			<p>littletotal:¥<%= littletotal %></p>
			</div>
			<div class="cart_item_delete">
				<form action="" method="post" class="">
			              <button type="submit" value="修正"  name="削除"></button>
			    </form>
			</div>
		</div>
	</div>
	<div class="item_alltotal">

	</div>
	<div class="itemconfirm">
		<form action="/HAYNTH/ItemRegistServlet" method="post" class="">
			
	     <input type="submit" value="登録">
	  </form>
	</div>
<% } %>

<%= alltotal %>円

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0