프로필사진
컨트롤러 구현(2) - 커맨드 객체, @ModelAttribute

2019. 11. 14. 23:32🔴 Spring

300x250

커맨드 객체 :
HTTP요청 파라미터를 저장한 객체
기본적으로 클래스 이름을 모델명으로 사용한다 (데이터 많을때 사용!)

@ModelAttribute를 사용하여 '모델명'을 설정할 수도 있다


Domain :
<OrderItem.java>

package com.bitcamp.mvc.domain;

public class OrderItem {
	private String itemId;
	private String number;
	private String remark;
	
	public String getItemId() {
		return itemId;
	}
	public void setItemId(String itemId) {
		this.itemId = itemId;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@Override
	public String toString() {
		return "OrderItem [itemId=" + itemId + ", number=" + number + ", remark=" + remark + "]";
	}
	
	
}

<Address.java>

package com.bitcamp.mvc.domain;

public class Address {
	
	private String zipcode;
	private String address1;
	private String address2;
	
	public String getZipcode() {
		return zipcode;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	public String getAddress1() {
		return address1;
	}
	public void setAddress1(String address1) {
		this.address1 = address1;
	}
	public String getAddress2() {
		return address2;
	}
	public void setAddress2(String address2) {
		this.address2 = address2;
	}
	
	@Override
	public String toString() {
		return "Address [zipcode=" + zipcode + ", address1=" + address1 + ", address2=" + address2 + "]";
	}
	
	
}

<OrderCommand.java>

package com.bitcamp.mvc.domain;

import java.util.List;

public class OrderCommand {
	
	private List<OrderItem> orderItems; // OrderItem.java
	private Address address; // Address.java
	
	public List<OrderItem> getOrderItems() {
		return orderItems;
	}
	public void setOrderItems(List<OrderItem> orderItems) {
		this.orderItems = orderItems;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	
	@Override
	public String toString() {
		return "OrderCommand [orderItems=" + orderItems + ", address=" + address + "]";
	}
	
	
	
}

 

<form.jsp> :
name을 살펴보면 orderItems는 배열인덱스로 데이터를 넣고,
address는 바로 참조해서 address의 zipcode, address1, address2에 데이터를 넣는다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	<form method="post">
		
		상품 1 - id : <input type="text" name="orderItems[0].itemId">		
		상품 1 - 개수 : <input type="text" name="orderItems[0].number">
		상품 1 - 주의 : <input type="text" name="orderItems[0].remark">
		
		<br>
		
		상품 2 - id : <input type="text" name="orderItems[1].itemId">		
		상품 2 - 개수 : <input type="text" name="orderItems[1].number">
		상품 2 - 주의 : <input type="text" name="orderItems[1].remark">
		
		<br>
		
		상품3 - id : <input type="text" name="orderItems[2].itemId">		
		상품 3 - 개수 : <input type="text" name="orderItems[2].number">
		상품 3 - 주의 : <input type="text" name="orderItems[2].remark">
		
		<br>
		
		배송지<br>
		우편번호 : <input type="text" name="address.zipcode">
		주소1: <input type="text" name="address.address1">
		주소2: <input type="text" name="address.address2">
		
		<br>
		
		<input type="submit" value="주문">
		
	</form>
	
</body>
</html>

 

<OrderController.java> :
post방식으로 보낼때 order메소드 실행 -> useBean의 setProperty처럼 OrderCommand의 변수에 자동 저장했고,
그리고 추가적으로@ModelAttribute로 이름 설정하고 있다.

package com.bitcamp.mvc.order;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bitcamp.mvc.domain.OrderCommand;
import com.bitcamp.mvc.domain.OrderItem;

@Controller
@RequestMapping("/order/order")
public class OrderController {
	
	@RequestMapping(method=RequestMethod.GET)
	public String getForm() {
		
		return "product/form";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String order(@ModelAttribute("orders") OrderCommand orders) {
	// post방식으로 보낼때 실행 -> useBean의 setProperty처럼 OrderCommand의 변수에 자동 저장
	// 추가적으로 @ModelAttribute로 이름 설정
	
		System.out.println(orders.getAddress());
		
		for(OrderItem item : orders.getOrderItems()) {
			System.out.println(item);
		}
		
		return "product/order";
	}
	
	
}

 

<order.jsp> : 
for문을 돌려서 orderItems배열의 값들을 출력하고,
address의 zipcode, address1, address2 값들을 출력했다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
	div{
		width : 250px;
		border: 3px solid;
		padding: 10px;
		margin: 5px;
	}
</style>
</head>
<body>
	
	<h1>주문내역</h1>
	
	<c:forEach items="${orders.orderItems}" var="item">
		<div>
			상품 id : ${item.itemId }<br>
			상품개수 : ${item.number }<br>
			주의사항 : ${item.remark }
		</div>
	</c:forEach>
	
	<h1>배송지</h1>
		<div>
			우편번호 : ${orders.address.zipcode } <br>
			주소1: ${orders.address.address1 }
			주소2: ${orders.address.address2 }
		</div>
	
</body>
</html>
300x250