배경 및 목표
본래는 주문하려는 상품과 주문정보 폼이 함께 나타나도록 하려 했는데 여러가지 문제에 봉착하고 해결하는데
시간이 지체되어서 급한대로 따로 주문자정보 테이블을 생성하여 주문페이지 다음으로 넘어가서 입력하도록 하였다
코드
주문정보를 입력할 테이블
test.jsp (임시로 이름을 test라 하였다)
<form action="/shop/test" method="post">
<input type="hidden" name="om_code" id="om_code" value="${om_code}" />
<table class="table table-bordered" >
<tbody>
<b>주문정보</b>
<tr>
<th>주문하시는 분</th>
<td><input type="text" name="oi_oname"></td>
</tr>
<tr>
<th>휴대전화</th>
<td><input type="text" name="oi_ophone"></td>
</tr>
</tbody>
</table>
<b>배송정보</b>
<table class="table table-bordered">
<tbody>
<tr>
<th>받으시는 분</th>
<td><input type="text" name="oi_rname"></td>
</tr>
<tr>
<th>주소</th>
<td>
<input type="text" name="oi_address1" id="sample4_postcode" placeholder="우편번호"><input type="button" onclick="showAddress();" value="우편번호"><br>
<input type="text" name="oi_address2" id="sample4_roadAddress" placeholder="도로명주소"><br>
<input type="text" name="oi_address3" id="sample4_jibunAddress" placeholder="지번주소">
<span id="guide" style="color:#999;display:none"></span>
<input type="text" name="oi_address5" id="sample4_extraAddress" placeholder="참고항목">
<input type="text" name="oi_address4" id="sample4_detailAddress" placeholder="상세주소">
</td>
</tr>
<tr>
<th>휴대전화</th>
<td><input type="text" name="oi_rphone"></td>
</tr>
<tr>
<th>배송메시지</th>
<td><input type="text" name="oi_message"></td>
</tr>
</tbody>
</table>
<input type="submit" class="form-control" value="등록">
</form>
※hidden 으로 om_code 값 전송한다(정확힌 orderinfo.om_code)
mapper.java
public void infoinsert(OrderinfoVO orderinfo);
mapper.xml
<insert id="infoinsert">
insert into tblorderinfo(oi_oname, oi_ophone, oi_address1, oi_address2,
oi_address3, oi_address4, oi_address5, oi_rphone, oi_message, om_code)
values(#{oi_oname}, #{oi_ophone}, #{oi_address1}, #{oi_address2},
#{oi_address3}, #{oi_address4}, #{oi_address5}, #{oi_rphone}, #{oi_message}, #{om_code})
</insert>
service.java
public void infoinsert(OrderinfoVO orderinfo);
serviceimpl.java
@Override
public void infoinsert(OrderinfoVO orderinfo) {
ordermapper.infoinsert(orderinfo);
}
controller
@GetMapping("/test")
public void test(HttpSession session, Model model, OrdermainVO ordermain) {
String m_id = (String)session.getAttribute("m_id");
String m_name = (String)session.getAttribute("m_name");
if(m_id != null) { // m_id가 있으면, 세션아이디를 이용해서 om_code 를 조회해야 함
// orderinfo 객체생성
OrderinfoVO orderinfo = new OrderinfoVO();
// ordermain의 om_code 부르기
orderinfo.setOm_code(ordermain.getOm_code());
model.addAttribute("orderinfo", orderinfo);
model.addAttribute("om_code", orderinfo.getOm_code());
}
}
@PostMapping("/test")
public String infoinsert(OrderinfoVO orderinfo) {
orderservice.infoinsert(orderinfo);
return "redirect:/shop/paymentsuccess";
}
결과
주문정보가 정상적으로 들어갔음을 확인가능하다
고찰
controller에서 getter와 setter를 이해하지 못하고 사용하다가 호되게 당했다 계속 에러가 나서 다시 기본부터 공부했다
order의 om_code를 불러와서 db에 넣는 것을 해결못하다가 form에 om_code값을 안넣어준것,
넣었는데 값이 없다고 떠서 확인해보니 mapper.xml에 om_code를 insert해주지 않아서 생긴 문제였다
'Framework > Spring' 카테고리의 다른 글
[에러] cannot add or update a child row: a foreign key constraint fails (0) | 2022.04.01 |
---|---|
[에러] 단위 테스트시, no runnable methods (0) | 2022.04.01 |
@Setter(onMethod_ = @Autowired) (0) | 2022.04.01 |
[에러] Expected one result (or null) to be returned by selectOne(), but found: 2 (0) | 2022.03.30 |
[정보] 이클립스에서 자바스크립트 하이라이트 설정하는법 (0) | 2022.03.24 |
댓글