87일차_Spring

서창민·2023년 7월 14일

Spring

목록 보기
14/15
post-thumbnail

23.07.14 87일차

Spring

  • 로그인
package com.korea.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.korea.k1.login.LoginService;
import com.korea.k1.board.koreaVO;

@Controller
public class LoginController {

	@Autowired
	private LoginService  service;
	
    @RequestMapping("/login.do")
	String  login( koreaVO vo , HttpSession session) 
			                            throws Exception { 

    	koreaVO  m = service.loginOne(vo);
    	System.out.println("mmmmmmmmmmmmmmmmmmmmm" + m);
    	if(m.getId()==null || m.getId().equals(null)) {
    		System.out.println("아이디가 존재하지 않습니다.");
    		return "/login/login.jsp";	
    	}else {
    		if(BCrypt.checkpw(vo.getPassword(), m.getPassword())) {
    			System.out.println("로그인 성공");
    			session.setAttribute("m", m);    			
        		return "/pdList.do";
    		}else {
    			System.out.println("암호가 일치하지 않습니다.");
        		return "/login/login.jsp";	
    		}
    	}	
	}
    
    @RequestMapping("/logout.do")
	String  logout( HttpSession session ) 
			                    throws Exception {
    	session.invalidate();
    	return "/index.jsp";     	
	}
}

로그인 컨트롤러를 사용하여 id의 존재유무를 조건으로하여 로그인 동작이 가능하게 했다.
암호화되어 DB에 저장된 Password와 가입시 입력한 Password의 비교로 로그인시 예외처리 하였다.

  • 로그인하여 장바구니에 저장
package com.korea.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.korea.k1.cart.CartServiceImpl;
import com.korea.k1.cart.CartVO;


@Controller
public class CartController {
	
	CartController(){
		System.out.println("===> 컨트롤러 확인");
	}
	
	@Autowired
	CartServiceImpl service;
	
	@RequestMapping(value = "/insertCart.do")
	public String insertCart(
			CartVO vo) throws Exception {
		System.out.println("===> insert " + vo);
		service.insert(vo);
		return "/CartList.do";
	}
	
	@RequestMapping(value = "/CartList.do")
	public String CartList(
			Model model,
			CartVO vo) throws Exception {
		model.addAttribute("li",service.cartList(vo));
		return "/cart/CartList.jsp";
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper 
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
	
<mapper namespace="cartDAO">

	
 <insert id="INSERT" parameterType="cart" >
   insert into tbl_cart2(cartId, custNo, productId, amount) 
   values(seq_tbl_cart2.nextval, #{custNo}, #{productId}, #{amount})
 </insert>
 
 <select id="SELECTALL" resultType="cart" >
    select cartId, c.productId, productName,
 	productPrice, amount, productImg as productImgStr
 	from tbl_cart2 c join tbl_product2 p
 	on c.productId = p.productId where custNo=#{custNo}
 	order by cartId
 </select>
</mapper>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:import url="/include/top.jsp"/>
<section>
<br>
<div align="center"><h2> 회원목록조회</h2></div>
<br>
  <table border=1 width=800 align="center">
   <tr align="center" style="font-weight: bold;">
    <td class="bold" width=80>CartId</td>
    <td width=100>ProductId</td>
    <td width=100>ProductName</td>
    <td width=100>ProductPrice</td>
    <td width=250>Amount</td>
   	<td width=100>productImgStr</td>
    
   </tr> 
    
  <c:forEach items="${li}" var="q">
   <tr align="center">
	    <td>${q.cartId}</td>
	    <td>${q.productId}</td>
	    <td>${q.productName}</td>
	    <td>${q.productPrice}</td>
	    <td>${q.amount}</td>	    
	    <td>${q.productImgStr}</td>
    </tr>
   </c:forEach>
  </table>
  <br>
  <form action="${path }/CartList.do">
  	<div align="center">
  		<select name=ch1>
  			<option value="custno">회원번호</option>
  			<option value="custname">회원이름</option>
  		</select>
  		<input type=text name=ch2>
  		<input type=submit value="검색"> 
 	</div>
 </form>
 <br>
</section>
<%@ include file ="/include/footer.jsp" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 
<c:import url="/include/top.jsp" />

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
	function delK(k){
		var path = "${path}";
		var dataStr = {
			productId: k
		};	  
		// 알림 메시지를 표시하여 삭제 여부 확인
		if (confirm("정말로 삭제하시겠습니까?")) {
			$.ajax({
				type: "post",
				url: path + "/pdDelete.do",
				data: dataStr,
				success: function(data) {		
					location.replace(path + "/pdList.do");  	
				}
			});
		}	  
	} 
	
	function listK(){
		location.href="${path}/pdList.do"
	}
</script>

<section>
	<br>
	<div align=center>
	<h2>쇼핑몰 상품등록 하기</h2>
		<form action=${path}/insertCart.do method="post" enctype="multipart/form-data" >
			<input type=text name=productId value="${o.productId}"/>
			<input type=text name=custNo value="${m.custno}"/>
			<table border=1 width=800 height=350 style="font-weight: bold;">
				<tr>
			  		<td align="center" width=100>번호</td>  
			   		<td> &emsp;<input type=text value="${o.productId}"/> </td>
			   		<td align=center rowspan=4><img src="${path}/product/files/${o.productImgStr}" width=100% height=90%></td></tr> 
				<tr>
			  		<td align="center">상품명</td>  
			   		<td> &emsp;<input type=text name=productName value="${o.productName}"/> </td></tr> 
			  	<tr>
				  	<td align="center">가격</td>  
					<td>&emsp;<input type=text name=productPrice value="${o.productPrice}"/> </td></tr>
  			  	<tr>
	  			  	<td align="center">설명</td>  
				   	<td> &nbsp;<textarea cols=40  rows=9 name=productDesc readonly>${o.productDesc} </textarea> </td></tr>
				<tr>
	  			  	<td align="center">수량</td>  
				   	<td> &nbsp;<input type=text name=amount min=1 max=5/></td></tr>
			  	<tr>
				  	<td align="center">등록날짜</td>  
	   				<td colspan=2>&emsp;<input type=text name=productDate value="${o.productDate}"/> </td></tr>
			  	<tr>
					<td colspan=3  align="center"> 
						<input  type=submit value="상품구매" >&emsp;
					    <input  type=button value="목록보기"onClick="listK()">&emsp;
					    <input  type=button value="상품삭제" onClick="delK('${o.productId}')">
				  	</td>
				</tr>
			</table>
		</form>
	</div>
	<br>
</section>
<c:import url="/include/footer.jsp" />

상품 상세화면에서 구매수량을 선택하여 구매동작시 장바구니 리스트 화면으로 이동되고, DB에 장바구니리스트에 등록한 데이터를 저장시킨다.

아이디비교하는 부분에서 호출하는 부분이 많이 어려웠다.
정확한 호출 위치와 데이터 흐름을 이해하도록 해야겠다.
화이팅

profile
QA Automation Engineer

0개의 댓글