반응형
ShopFrontController에서 전송된 요청을 파악하여 각 요청을 처리하는 Action 클래스 객체의 exeucte 메소드를 호출하게 된다.
📌상품 등록 페이지를 보여달라는 요청 처리
ClothesRegistFormAction.java
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vo.ActionForward;
public class ClothesRegistFormAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
ActionForward forward = new ActionForward("clothesRegistForm.jsp", false);
return forward;
}
}
📌상품 등록 요청 처리
ClothesRegistAction.java
package action;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import service.ClothesRegistService;
import vo.ActionForward;
import vo.Clothes;
public class ClothesRegistAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception {
ClothesRegistService DogRegistService = new ClothesRegistService();
//파일 업로드될 서버 상의 물리적인 경로
String realFolder = "";
//파일 업로드될 서버 상의 물리적인 경로
String saveFolder = "/images";
String encType = "UTF-8";
//한번에 업로드 할 수 있는 파일의 크기 5Mbyte로 지정
int maxSize = 5*1024*1024;
ServletContext context = request.getServletContext();
realFolder = context.getRealPath(saveFolder);
MultipartRequest multi = new MultipartRequest(request,
realFolder, maxSize, encType,
new DefaultFileRenamePolicy());
String image = multi.getFilesystemName("image");
Clothes cloth = new Clothes(
0,
multi.getParameter("name"),
Integer.parseInt(multi.getParameter("price")),
image,
multi.getParameter("size"),
multi.getParameter("content"),
0);
boolean isRegistSuccess = ClothesRegistService.registClothes(cloth);
ActionForward forward = null;
if(isRegistSuccess){
forward = new ActionForward("clothesList.shop", true);
}else{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<script>");
out.println("alert('등록실패');");
out.println("history.back();");
out.println("</script>");
}
return forward;
}
}
📌상품 목록 보기 요청 처리
ClothesListAction.java
package action;
import java.util.ArrayList;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.ClothesListService;
import vo.ActionForward;
import vo.Clothes;
public class ClothesListAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
ArrayList<String> todayImageList = new ArrayList<String>();
//사이트에서 오늘 본 상품이 있다면 각 상품의 이미지가 쿠키로 저장되어 있음.
Cookie[] cookieArray = request.getCookies();
if(cookieArray != null){
for (int i = 0; i < cookieArray.length; i++) {
if(cookieArray[i].getName().startsWith("today")){
todayImageList.add(cookieArray[i].getValue());
}
}
}
ClothesListService clothesListService = new ClothesListService();
ArrayList<Clothes> clothesList = clothesListService.getClothesList();
request.setAttribute("clothesList", clothesList);
request.setAttribute("todayImageList", todayImageList);
ActionForward forward = new ActionForward("clothesList.jsp", false);
return forward;
}
}
📌상품 상세 보기 요청 처리
ClothesViewAction.java
package action;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.ClothesViewService;
import vo.ActionForward;
import vo.Clothes;
public class ClothesViewAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception {
ClothesViewService clothesViewService = new ClothesViewService();
int id = Integer.parseInt(request.getParameter("id"));
Clothes cloth = clothesViewService.getDogView(id);
request.setAttribute("cloth", cloth);
//today+상품id 로 쿠키 이름으로 지정 후 쿠키 객체 생성하여 저장한다.
Cookie todayImageCookie = new Cookie("today"+id, cloth.getImage());
todayImageCookie.setMaxAge(60*60*24); //쿠키 저장 시간을 24시간으로 지정
response.addCookie(todayImageCookie);
ActionForward forward = new ActionForward("clothesView.jsp", false);
return forward;
}
}
📌장바구니 담기 요청을 처리
ClothesCartAddAction.java
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.ClothesCartAddService;
import vo.ActionForward;
import vo.Clothes;
public class ClothesCartAddAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
ClothesCartAddService clothesCartAddService = new ClothesCartAddService();
//장바구니 항목으로 추가될 상품의 아이디
int id = Integer.parseInt(request.getParameter("id"));
//장바구니 항목으로 추가될 상품의 정보를 얻어옴.
Clothes cartClothes = clothescartAddService.getCartClotehs(id);
//장바구니에 상품 추가
clothesCartAddService.addCart(request,cartClothes);
//장바구니에 추가 후, 장바구니 목록 보기 요청
ActionForward forward = new ActionForward("clothesCartList.shop", true);
return forward;
}
}
📌장바구니 목록 List 보기 요청처리
ClothesCartListAction.java
package action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.ClothesCartListService;
import vo.ActionForward;
import vo.Cart;
public class ClothesCartListAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
ClothesCartListService clothesCartListService = new ClothesCartListService();
ArrayList<Cart> cartList = clothesCartListService.getCartList(request);
//총금액계산
int totalMoney = 0;
int money = 0 ;
for (int i = 0; i < cartList.size(); i++) {
money = cartList.get(i).getPrice()*cartList.get(i).getQty(); //가격 * 양
totalMoney += money;
}
request.setAttribute("totalMoney", totalMoney);
request.setAttribute("cartList", cartList);
ActionForward forward = new ActionForward("clothesCartList.jsp", false);
return forward;
}
}
📌장바구니 삭제 요청 처리
ClothesCartRemoveAction.java
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.ClothesCartRemoveService;
import vo.ActionForward;
public class ClothesCartRemoveAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception {
//삭제할 항목(item) 넘겨 받음
String[] itemArray = request.getParameterValues("remove");
ClothesCartRemoveService clothesCartRemoveService = new ClothesCartRemoveService();
clothesCartRemoveService.cartRemove(request,itemArray);
//장바구니 목록보기 요청
ActionForward forward = new ActionForward("clothesCartList.shop",true);
return forward;
}
}
📌장바구니 항목 수량 증가/감소 요청
ClothesCartQtyUpAction.java
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.DogCartQtyUpService;
import vo.ActionForward;
public class ClothesCartQtyUpAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception {
//장바구니 항목의 식별자를 상품의 이름(name) 값을 사용한 것이다.
String name = request.getParameter("name");
ClothesCartQtyUpService clothesCartQtyUpService = new ClothesCartQtyUpService();
clothesCartQtyUpService.upCartQty(name,request);
ActionForward forward = new ActionForward("clothesCartList.shop", true);
return forward;
}
}
ClothesCartQtyDownAction.java
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.ClothesCartQtyDownService;
import vo.ActionForward;
public class ClothesCartQtyDownAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception {
String name = request.getParameter("name");
ClothesCartQtyDownService clothesCartQtyDownService = new ClothesCartQtyDownService();
clothesCartQtyDownService.downCartQty(name,request);
ActionForward forward = new ActionForward("clothesCartList.shop",true);
return forward;
}
}
📌장바구니 목록에서 가격 범위 설정하여 상품 출력 요청
ClothesCartSearchAction.java
package action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import service.ClothesCartSearchService;
import vo.ActionForward;
import vo.Cart;
public class ClothesCartSearchAction implements Action {
@Override
public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception {
ClothesCartSearchService clothesCartSearchService = new ClothesCartSearchService();
int startMoney = Integer.parseInt(request.getParameter("startMoney"));
int endMoney = Integer.parseInt(request.getParameter("endMoney"));
ArrayList<Cart> cartList =
clothesCartSearchService.getCartSearchList(startMoney,endMoney,request);
request.setAttribute("cartList", cartList);
request.setAttribute("startMoney", startMoney);
request.setAttribute("endMoney", endMoney);
int totalMoney = 0;
int money = 0 ;
for (int i = 0; i < cartList.size(); i++) {
money = cartList.get(i).getPrice()*cartList.get(i).getQty();
totalMoney += money;
}
request.setAttribute("totalMoney", totalMoney);
ActionForward forward = new ActionForward("clothesCartList.jsp", false);
return forward;
}
}
반응형
'JSP & Servlet > 실습' 카테고리의 다른 글
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - DAO (0) | 2022.07.01 |
---|---|
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - Service (0) | 2022.07.01 |
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - controller (0) | 2022.07.01 |
[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - 테이블 생성 및 vo(Value Object) + 기본 세팅 (0) | 2022.07.01 |
[JSP & Servlet] 로그인 (feat. 세션 & 쿠키) - controller / service (0) | 2022.06.30 |