RequestMapping
myshop에 controller는 user로그인 되었을때 실행되어야하므로 /user가 공통적으로 붙어야한다.
이런 경우 class앞에 user을 매핑하면 /user/cartAdd로 Mapping된다.
기존에는 RequestMapping을 사용해서 했지만 현재는
@PostMapping을 통해 method를 표시해줄 수 있다.
package com.tis.myshop;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.tis.domain.CartVO;
import com.tis.service.ShopService;
import lombok.extern.log4j.Log4j;
@Controller
@Log4j
@RequestMapping("/user")
public class CartController {
@Resource(name="shopServiceImpl")
private ShopService shopSvc;
@RequestMapping(value="/cartAdd",method=RequestMethod.POST)
public String addCart(Model model, @RequestParam(defaultValue="0") int pnum,
@RequestParam(defaultValue="0") int oqty) {
log.info("shopSvc= "+shopSvc);
//로그인한 사람의 회원번호를 가져와서 해당 회원의 장바구니 목록에 추가
log.info("pnum= "+pnum+"oqty= "+oqty);
if(pnum==0||oqty==0) {
return "redirect:index";
}
CartVO cartVo = new CartVO();
cartVo.setPnum(pnum);
cartVo.setOqty(oqty);
cartVo.setIdx(11); //로그인한 회원의 회원번호로 대체 예정
int n = shopSvc.addCart(cartVo);
String str=(n>0)? "장바구니 담기 성공":"실패";
String loc=(n>0)? "cartList":"javascript:history.back";
model.addAttribute("message",str);
model.addAttribute("loc",loc);
return "msg";
//return "shop/cartList";
}
@RequestMapping(value="/cartList", method=RequestMethod.GET)
public String cartList(Model model) {
//로그인한 회원의 회원번호 알아내기
int idx = 11; //회원번호
//특정 회원의 장바구니 목록 가져오기
List<CartVO> cList=this.shopSvc.selectCartView(idx);
//장바구니 총액, 총 포인트 구하기
CartVO cvo = this.shopSvc.getCartTotal(idx);
model.addAttribute("cartList",cList);
if(cvo!=null) {
model.addAttribute("cartTotalPrice",cvo.getCartTotalPrice());
model.addAttribute("cartTotalPoint",cvo.getCartTotalPoint());
}
return "shop/cartList";
}
//장바구니 삭제
//@RequestMapping(value="/cartDel",method=RequestMethod.POST)
@PostMapping("/cartDel")
public String cartDelete(Model model,@RequestParam(defaultValue="0") int cartNum) {
log.info("carNum: "+cartNum);
if(cartNum==0) {
return "redirect:index";
}
int n=this.shopSvc.delCart(cartNum);
return "redirect:cartList";
}
}
변수 객체로 받기
@ModelAttribute로 받으면 setter와 getter가 있으면 변수를 담은 객체를 생성해서 넘겨준다.
@PostMapping("/cartEdit")
//public String cartEdit(@RequestParam(defaultValue="0") int cartNum,@RequestParam(defaultValue="-1") int oqty) {
public String cartEdit(Model model,@ModelAttribute("cartVo") CartVO cartVo) {
log.info("cartVo:"+cartVo);
int oqty=cartVo.getOqty();
int cartNum=Integer.parseInt(cartVo.getCartNum());
if(oqty>0) {
//수량 수정
this.shopSvc.editCart(cartVo);
}else if(oqty==0) {
//삭제 처리
this.shopSvc.delCart(cartNum);
}else {
//수량이 음수일 경우
return util.addMsgBack(model, "수량이 음수입니다.");
}
return "redirect:cartList";
}
Component
Resource도 아니고 Service도 아니면 상위항목인 Component로 annotation
com.tis.common.util
servlet-context에 등록하고
class에 @Component를 붙여준다.
package com.tis.common.util;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
@Component
public class CommonUtil {
public String addMsgLoc(Model m , String msg, String loc) {
m.addAttribute("msg",msg);
m.addAttribute("loc",loc);
return "msg";
// WEB-INF/views/msg.jsp
}
public String addMsgBack(Model m , String msg) {
m.addAttribute("msg",msg);
m.addAttribute("loc","javascript:history.back()");
return "msg";
// WEB-INF/views/msg.jsp
}
}
동적 MyBatis
Spring에서는 동적인 sql문에서 작성 가능하다.
<?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="com.tis.mapper.UserMapper">
<select id="findUser" parameterType="User" resultType="User">
select * from member
<where>
<if test="userid!=null and userid !=''">
userid=#{userid}
</if>
<if test="name!=null and name!=''">
name=#{name}
</if>
</where>
</select>
</mapper>
'개발자 > 국비지원 SW' 카테고리의 다른 글
국비지원 97일차 - VueJS directive (0) | 2020.09.10 |
---|---|
국비지원 96일차 - 온라인강의 환경설정, Vue.JS, MVVM패턴, Vue객체 (0) | 2020.09.09 |
국비지원 94일차 - NodeJS mychat 입장, 퇴장, 채팅 (0) | 2020.08.27 |
국비지원 93일차 - Spring MVC 요청 처리 과정 (0) | 2020.08.26 |
국비지원 92일차 - NodeJS socket기능, mychat 방생성 (0) | 2020.08.25 |