controller & jsp
1. 고객목록화면
1) controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Controller public class CustomerListController { @Autowired private CustomerService customerService; @RequestMapping (value= "/customer" , method= GET) public String showAllCustomers(Model model){ List<Customer>customers = customerService.findAll(); model.addAttribute( "customers" ,customers); return "customer/list" ; } @RequestMapping (value= "/" , method=GET) public String home(){ return "forward:/customer" ; } } |
- 컨텍스트 루트(/)로 엑세스 했을 시 /customer에 포워딩 되도록 변경함
2) jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < head > < meta http-equiv = "Content-Type" content = "text/html; charset=EUC-KR" > < title >Insert title here</ title > </ head > < h2 >Client list</ h2 > < table border = "1" > < tr > < th >Id</ th > < th >name</ th > < th >address</ th > < th >email</ th > < td ></ td > </ tr > < c:forEach items = "${customers}" var = "customer" > < tr > < td >< c:out value = "${customer.id}" /></ td > < td >< c:out value = "${customer.name}" /></ td > < td >< c:out value = "${customer.address}" /></ td > < td >< c:out value = "${customer.email}" /></ td > < td > < c:url value = "/customer/${customer.id }" var = "url" /> < a href = "${url }" >detail</ a > < c:url value = "/customer/${customer.id }/edit" var = "url" /> < a href = "${url }" >modify</ a > </ td > </ tr > </ c:forEach > </ table > |
- View와 뷰리졸버의 설명처럼 Model 오브젝트로 설정한 오브젝트는 스프링MVC가 자동으로 HttpServeltRequest로 설정
2. 고객정보화면표시처리
1) Controller
1 2 3 4 5 6 | @RequestMapping (value= "/customer/{customer}" ,method= GET) public String showCustomerDetail( @PathVariable int customerId,Model model) throws DataNotFoundException{ Customer customer = customerService.findById(customerId); model.addAttribute( "customer" ,customer); return "customer/detail" ; } |
2) jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> < head > < meta http-equiv = "Content-Type" content = "text/html; charset=EUC-KR" > < title >Insert title here</ title > </ head > < h1 >Client info</ h1 > < dl > < dt >name</ dt > < dd >< c:out value = "${customer.name }" /></ dd > < dt >address</ dt > < dd >< c:out value = "${customer.address }" /></ dd > < dt >email</ dt > < dd >< c:out value = "${customer.email }" /></ dd > </ dl > < c:url value = "/customer" var = "url" /> < a href = "${url }" >list</ a > |
'FullStack > SimpleSpringMVC' 카테고리의 다른 글
[SpringMVC] 레이어 구조 (0) | 2016.08.21 |
---|---|
[SpringMVC] web.xml 설정 (0) | 2016.08.21 |
[SpringMVC] MVC의 Bean 정의 파일 (0) | 2016.08.18 |
[SpringMVC] Bean 정의 파일 위치 (0) | 2016.08.18 |
[SpringMVC] 비즈니스 로직 Bean 정의 파일 (0) | 2016.08.16 |