SpringMVC

woomยท2023๋…„ 2์›” 21์ผ
0

Framework

๋ชฉ๋ก ๋ณด๊ธฐ
12/20
post-thumbnail

๐ŸŒผ MVC Model2

  • web application ๊ฐœ๋ฐœ ์‹œ Servlet์ด ์‚ฌ์šฉ๋จ

  • Controller : ์š”์ฒญ ํด๋ผ์ด์–ธํŠธ์˜ URL ์ •๋ณด๋ฅผ ๋ถ„์„ํ•˜์—ฌ URL์— ํ•ด๋‹นํ•˜๋Š” ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ์ ์ ˆํ•œ JSPํŽ˜์ด์ง€๋กœ ํ™”๋ฉด(View)์„ ์ œ์–ดํ•˜๋Š” ๊ตฌ์กฐ


๐Ÿ“• DispatcherServlet

  • ์ปจํŠธ๋กค๋Ÿฌ(Controller) : ํด๋ผ์ด์–ธํŠธ์˜ ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›์•„ URL ์ฃผ์†Œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค(Model)์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ฐ์ดํƒ€ ์ฒ˜๋ฆฌ ํ›„ JSP ๋ฌธ์„œ(View)๋กœ ์Šค๋ ˆ๋“œ๋ฅผ ์ด๋™ํ•˜์—ฌ ์‘๋‹ต๋˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ์˜ ํ๋ฆ„ ์ œ์–ดํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์„œ๋ธ”๋ฆฟ

  • ์ปจํŠธ๋กค๋Ÿฌ ์—ญํ™œ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค

    • ํด๋ผ์ด์–ธํŠธ์˜ ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋‹จ์ผ ์ง„์ž…์ ์˜ ์—ญํ™œ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์„œ๋ธ”๋ฆฟ (Front Controller Pattern)
    • [web.xml] ํŒŒ์ผ์—์„œ ํด๋ž˜์Šค๋ฅผ ์„œ๋ธ”๋ฆฟ(์›นํ”„๋กœ๊ทธ๋žจ)์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์˜ ๋ชจ๋“  ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก URL ์ฃผ์†Œ์˜ ํŒจํ„ด์„ ์„ค์ •
  1. ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ URL ์ฃผ์†Œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
    • request.getRequestURI() : url ์ฃผ์†Œ ๋ฐ˜ํ™˜ (/string/list.itwill)
    • request.getContextPath() : ์ปจํ…์ŠคํŠธ ๊ฒฝ๋กœ (์ตœ์ƒ์œ„๊ฒฝ๋กœ /string )
    • requestURI.substring(contextPath.length()) : ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋Œ€ํ•œ ์š”์ฒญ๊ฐ’ (list.itwill)
  1. ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ด์šฉํ•˜์—ฌ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค(Model)์˜ ๊ฐ์ฒด๋ฅผ ์ œ๊ณต๋ฐ›์•„ ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ํ›„ ์ถœ๋ ฅ(View)๊ด€๋ จ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
    • HandlerMapping ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด ์ƒ์„ฑ
    • ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ๊ณผ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๊ฐ€ ์—”ํŠธ๋ฆฌ๋กœ ์ €์žฅ๋œ Map ๊ฐ์ฒด ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • HandlerMapping ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
    • ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  JSP ๋ฌธ์„œ์˜ ์ด๋ฆ„(ViewName)์„ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
  1. JSP ๋ฌธ์„œ๋กœ ํฌ์›Œ๋“œ ์ด๋™ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๊ฐ€ ์‘๋‹ต๋˜๋„๋ก ์ฒ˜๋ฆฌ
    • ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ์˜ ๋ฐ˜ํ™˜๊ฐ’(ViewName)์„ ์ด์šฉํ•˜์—ฌ JSP ๋ฌธ์„œ์˜ ๊ฒฝ๋กœ๋ฅผ ์™„์„ฑํ•˜์—ฌ ํฌ์›Œ๋“œ ์ด๋™
    • ViewResolver ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด ์ƒ์„ฑ
package xyz.itwill09.mvc;

public class DispatcherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	//ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ž๋™ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ์„ ์–ธ
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ URL ์ฃผ์†Œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
		String requestURI=request.getRequestURI();
		String contextPath=request.getContextPath();
		String command=requestURI.substring(contextPath.length());
		
		//2.ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ด์šฉํ•˜์—ฌ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค(Model)์˜ ๊ฐ์ฒด๋ฅผ ์ œ๊ณต๋ฐ›์•„ ๊ฐ์ฒด์˜
		//๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ์ž‘์—…
		/*
		//์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ฐธ์กฐ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ๋ชจ๋“  ์ž์‹ํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ €์žฅ ๊ฐ€๋Šฅ
		Controller controller=null;
		//ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ๋น„๊ตํ•˜์—ฌ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ
		if(command.equals("/list.itwill")) {
			controller=new ListController();
		} else if(command.equals("/view.itwill")) {
			controller=new ViewController();
		}
		*/
		
		//HandlerMapping ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด ์ƒ์„ฑ
		// => ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ๊ณผ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๊ฐ€ ์—”ํŠธ๋ฆฌ๋กœ ์ €์žฅ๋œ Map ๊ฐ์ฒด ์‚ฌ์šฉ ๊ฐ€๋Šฅ
		HandlerMapping handlerMapping=new HandlerMapping();
		//HandlerMapping ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ
		//์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
		Controller controller=handlerMapping.getController(command);
		
		//์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  JSP ๋ฌธ์„œ์˜ 
		//์ด๋ฆ„(ViewName)์„ ๋ฐ˜ํ™˜๋ฐ›์•„ ์ €์žฅ
		String viewName=controller.handleRequest(request, response);
		
		//3.JSP ๋ฌธ์„œ๋กœ ํฌ์›Œ๋“œ ์ด๋™ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๊ฐ€ ์‘๋‹ต๋˜๋„๋ก ์ฒ˜๋ฆฌ
		// => ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ์˜ ๋ฐ˜ํ™˜๊ฐ’(ViewName)์„ ์ด์šฉํ•˜์—ฌ JSP ๋ฌธ์„œ์˜ ๊ฒฝ๋กœ๋ฅผ ์™„์„ฑํ•˜์—ฌ ํฌ์›Œ๋“œ ์ด๋™
		//ViewResolver ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด ์ƒ์„ฑ
		ViewResolver viewResolver=new ViewResolver();
		String view=viewResolver.getView(viewName);//์‘๋‹ตํ•  JSP ๋ฌธ์„œ์˜ ๊ฒฝ๋กœ ์™„์„ฑ
		request.getRequestDispatcher(view).forward(request, response);//jsp๋ฌธ์„œ๋กœ ํฌ์›Œ๋“œ ์ด๋™	
	}
}





๐Ÿฃ VO class (Member)

  • ํšŒ์›์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค (VO ํด๋ž˜์Šค)
package xyz.itwill09.mvc;

import lombok.AllArgsConstructor;
import lombok.Data;

@AllArgsConstructor//๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์„ ์–ธ๋œ ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜ (๋ชจ๋“  ํ•„๋“œ์— ๋Œ€ํ•œ ์˜์กด์„ฑ ์ฃผ์ž…)
@Data
public class Member {
	private String id;
	private String name;
	private String address;
}





๐Ÿ“™ HandlerMapping

  • ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ๊ณผ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ Map ๊ฐ์ฒด์˜ ์—”ํŠธ๋ฆฌ(Entry)๋กœ ์ €์žฅํ•˜์—ฌ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์˜ ํด๋ž˜์Šค

  • Map ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํ•„๋“œ

    • ์—”ํŠธ๋ฆฌ์˜ ์ œ๋„ค๋ฆญ์œผ๋กœ ๋งตํ‚ค๋Š” ์š”๊ตฌ์‚ฌํ•ญ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ String ํด๋ž˜์Šค๋กœ ์„ค์ •ํ•˜๊ณ  ๋งต๊ฐ’์€ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด(model)๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ Controller ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์„ค์ •
  • ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•œ ํ•„๋“œ ์ดˆ๊ธฐํ™” (Map ๊ฐ์ฒดํ•˜์—ฌ ์—”ํŠธ๋ฆฌ ์ถ”๊ฐ€)

  • ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ(MapKey)์— ๋Œ€ํ•œ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด(MapValue)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ


import java.util.HashMap;
import java.util.Map;

public class HandlerMapping {
	//Map ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ํ•„๋“œ
	// => ์—”ํŠธ๋ฆฌ์˜ ์ œ๋„ค๋ฆญ์œผ๋กœ ๋งตํ‚ค๋Š” ์š”๊ตฌ์‚ฌํ•ญ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ String ํด๋ž˜์Šค๋กœ ์„ค์ •ํ•˜๊ณ 
	//๋งต๊ฐ’์€ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ Controller ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์„ค์ •
	private Map<String, Controller> mappings;
	
	//์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•œ ํ•„๋“œ ์ดˆ๊ธฐํ™” - Map ๊ฐ์ฒดํ•˜์—ฌ ์—”ํŠธ๋ฆฌ ์ถ”๊ฐ€
	public HandlerMapping() {
		mappings=new HashMap<String, Controller>();
		mappings.put("/list.itwill", new ListController());
		mappings.put("/view.itwill", new ViewController());
	}
	
	//ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ(MapKey)์— ๋Œ€ํ•œ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด(MapValue)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
	public Controller getController(String command) {
		return mappings.get(command);
	}
}





๐Ÿ“’ Controller

  • ๋ชจ๋ธ ์—ญํ™œ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ๋ฐ˜๋“œ์‹œ ์ƒ์† ๋ฐ›์•„์•ผ ๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค

    • ๋ชจ๋“  ๋ชจ๋ธ ํด๋ž˜์Šค์— ๋™์ผํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ž‘์„ฑ๋˜๋„๋ก ๊ทœ์น™ ์ œ๊ณต
    • ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ชจ๋ธ ํด๋ž˜์Šค์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์‰ฝ๊ฒŒ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์ธํ„ฐํŽ˜์ด์Šค ์‚ฌ์šฉ
  • ๋ชจ๋“  ๋ชจ๋ธ ํด๋ž˜์Šค์—์„œ ๋ฐ˜๋“œ์‹œ ์ž‘์„ฑํ•  ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”์ƒ๋ฉ”์†Œ๋“œ๋กœ ์„ ์–ธ


package xyz.itwill09.mvc;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Controller {
	//๋ชจ๋“  ๋ชจ๋ธ ํด๋ž˜์Šค์—์„œ ๋ฐ˜๋“œ์‹œ ์ž‘์„ฑํ•  ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”์ƒ๋ฉ”์†Œ๋“œ๋กœ ์„ ์–ธ 
	String handleRequest(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException;
}





๐ŸŒธ Model1 (list.itwill)

  • ๋ชจ๋ธ(Model) : ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค

    • ๋ชจ๋ธ ์—ญํ™œ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค - ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๊ฐ€ ์„ ์–ธ๋œ ์ธํ„ฐํŽ˜์ด์Šค(Controller)๋ฅผ ์ƒ์†๋ฐ›์•„ ์ž‘์„ฑ
    • ํ•˜๋‚˜์˜ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ (Command Pattern)
    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ [/list.itwill]์˜ URL ์ฃผ์†Œ๋กœ ์š”์ฒญํ•œ ๊ฒฝ์šฐ ์ปจํŠธ๋กค๋Ÿฌ์— ์˜ํ•ด ๋™์ž‘๋  ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค
  • ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ : ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์„ ์ž‘์„ฑํ•œ ๋ฉ”์†Œ๋“œ

    • ์Šค๋ ˆ๋“œ๊ฐ€ ์ด๋™๋˜์–ด ์‘๋‹ต๋  JSP ๋ฌธ์„œ์˜ ์ด๋ฆ„(ViewName)์„ ๋ฐ˜ํ™˜
    • ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ช…๋ น - ๋ฐ์ดํƒ€ ์ฒ˜๋ฆฌ : Service ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
    • ์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ JSP ๋ฌธ์„œ์— ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด request ์†์„ฑ๊ฐ’์œผ๋กœ ์ €์žฅ
    • ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณต๋ฐ›์•„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•  JSP ๋ฌธ์„œ์˜ ์ด๋ฆ„(ViewName) ๋ฐ˜ํ™˜

package xyz.itwill09.mvc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//๋ชจ๋ธ ์—ญํ™œ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค - ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๊ฐ€ ์„ ์–ธ๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์ž‘์„ฑ
// => ํ•˜๋‚˜์˜ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ์ฒ˜๋ฆฌ - Command Pattern
// => ํด๋ผ์ด์–ธํŠธ๊ฐ€ [/list.itwill]์˜ URL ์ฃผ์†Œ๋กœ ์š”์ฒญํ•œ ๊ฒฝ์šฐ ์ปจํŠธ๋กค๋Ÿฌ์— ์˜ํ•ด ๋™์ž‘๋  ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค
public class ListController implements Controller {
	//์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ : ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์„ ์ž‘์„ฑํ•œ ๋ฉ”์†Œ๋“œ
	// => ์Šค๋ ˆ๋“œ๊ฐ€ ์ด๋™๋˜์–ด ์‘๋‹ต๋  JSP ๋ฌธ์„œ์˜ ์ด๋ฆ„(ViewName)์„ ๋ฐ˜ํ™˜
	@Override
	public String handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//์š”์ฒญ ์ฒ˜๋ฆฌ ๋ช…๋ น - ๋ฐ์ดํƒ€ ์ฒ˜๋ฆฌ : Service ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
		List<Member> memberList=new ArrayList<Member>();
		memberList.add(new Member("abc", "ํ™๊ธธ๋™", "์„œ์šธ์‹œ ๊ฐ•๋‚จ๊ตฌ"));
		memberList.add(new Member("opq", "์ž„๊บฝ์ •", "์ธ์ฒœ์‹œ ์›”๋ฏธ๊ตฌ"));
		memberList.add(new Member("xyz", "์ „์šฐ์น˜", "์ˆ˜์›์‹œ ํŒ”๋‹ฌ๊ตฌ"));
		
		//์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ JSP ๋ฌธ์„œ์— ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด request ์†์„ฑ๊ฐ’์œผ๋กœ ์ €์žฅ 
		request.setAttribute("memberList", memberList);
		
		//์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ œ๊ณต๋ฐ›์•„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•  JSP ๋ฌธ์„œ์˜ ์ด๋ฆ„(ViewName) ๋ฐ˜ํ™˜
		return "member_list";
	}
}





๐ŸŒธ Model2 (view.itwill)

  • ๋ชจ๋ธ ์—ญํ™œ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค

    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ [/view.itwill]์˜ URL ์ฃผ์†Œ๋กœ ์š”์ฒญํ•œ ๊ฒฝ์šฐ ์ปจํŠธ๋กค๋Ÿฌ์— ์˜ํ•ด ๋™์ž‘๋  ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค

package xyz.itwill09.mvc;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//๋ชจ๋ธ ์—ญํ™œ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค
// => ํด๋ผ์ด์–ธํŠธ๊ฐ€ [/view.itwill]์˜ URL ์ฃผ์†Œ๋กœ ์š”์ฒญํ•œ ๊ฒฝ์šฐ ์ปจํŠธ๋กค๋Ÿฌ์— ์˜ํ•ด ๋™์ž‘๋  ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค
public class ViewController implements Controller {
	@Override
	public String handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Member member=new Member("abc", "ํ™๊ธธ๋™", "์„œ์šธ์‹œ ๊ฐ•๋‚จ๊ตฌ");
		
		request.setAttribute("member", member);
		
		return "member_view";
	}
	
}





๐Ÿ“— ViewResolver

  • ์‘๋‹ต ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค

  • JSP ๋ฌธ์„œ์˜ ์ด๋ฆ„(ViewName)์„ ์ „๋‹ฌ๋ฐ›์•„ JSP ๋ฌธ์„œ์˜ ๊ฒฝ๋กœ๋ฅผ ์™„์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ

package xyz.itwill09.mvc;

public class ViewResolver {
	//JSP ๋ฌธ์„œ์˜ ์ด๋ฆ„(ViewName)์„ ์ „๋‹ฌ๋ฐ›์•„ JSP ๋ฌธ์„œ์˜ ๊ฒฝ๋กœ๋ฅผ ์™„์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ 
	public String getView(String viewName) {
		return "/WEB-INF/mvc/"+viewName+".jsp";
	}
}





๐Ÿฃ ์‘๋‹ต (member_list)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    
<%-- ํšŒ์›๋ชฉ๋ก์„ ์ œ๊ณต๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” JSP ๋ฌธ์„œ --%>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>ํšŒ์›๋ชฉ๋ก</h1>
	<hr>
	<c:forEach var="member" items="${memberList }">
		<p>์•„์ด๋”” = ${member.id }, ์ด๋ฆ„ = ${member.name }, ์ฃผ์†Œ = ${member.address }</p>
	</c:forEach>
</body>
</html>





๐Ÿฃ ์‘๋‹ต (member_view)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%-- ํšŒ์›์ •๋ณด๋ฅผ ์ œ๊ณต๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” JSP ๋ฌธ์„œ --%>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>ํšŒ์›์ •๋ณด</h1>
	<hr>
	<p>์•„์ด๋”” = ${member.id }, ์ด๋ฆ„ = ${member.name }, ์ฃผ์†Œ = ${member.address }</p>
</body>
</html>





๐Ÿ“˜ web.xml

  • context-param : ๋ชจ๋“  ์›นํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์— ํ•„์š”ํ•œ ๊ฐ’์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • contextConfigLocation ์ด๋ฆ„์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(WebApplocationContext ๊ฐ์ฒด)์—๊ฒŒ Spring Bean Configuration File์˜ ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณต
    • ๋ชจ๋“  ์›นํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Spring Bean๋ฅผ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ Spring Bean Configuration File
    • ๋ชจ๋“  Front Controller ์—ญํ• ์„ ํ•˜๋Š” ์›นํ”„๋กœ๊ทธ๋žจ์—๊ฒŒ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด Spring Bean์œผ๋กœ ๋“ฑ๋ก
  • listener : WAS ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋  ๋•Œ Listener ํด๋ž˜์Šค๋ฅผ ์ œ๊ณต๋ฐ›์•„ ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • Listener ํด๋ž˜์Šค : ServletContextListener ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ž์‹ํด๋ž˜์Šค
    • contextInitialized ๋ฉ”์†Œ๋“œ : Listener ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ์ž๋™ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ (์ดˆ๊ธฐํ™” ์ž‘์—…)
    • contextDestroyed ๋ฉ”์†Œ๋“œ : Listener ๊ฐ์ฒด ์†Œ๋ฉธ ์ „ ์ž๋™ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ (๋งˆ๋ฌด๋ฆฌ ์ž‘์—…)
  • listener-class : ServletContextListener ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ž์‹ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • WAS ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋  ๋•Œ ContextLoaderListener ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๊ณ  contextInitialized ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ดˆ๊ธฐํ™” ์ž‘์—…
    • ContextLoaderListener ํด๋ž˜์Šค์˜ contextInitialized ๋ฉ”์†Œ๋“œ์—์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(Spring Container)๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Spring Bean Configuration File์„ ์ œ๊ณต๋ฐ›์•„ Spring Bean์œผ๋กœ ๋“ฑ๋ก๋œ ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ
    • Listener ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ์— ์˜ํ•ด ์ƒ์„ฑ๋œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(WebApplicationContext ๊ฐ์ฒด)๋Š” ๋ชจ๋“  ์›น์ž์›(์„œ๋ธ”๋ฆฟ ๋ฐ ํ•„ํ„ฐ)์—๊ฒŒ ๊ณต์œ ๋˜์–ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ (Spring Bean ์ œ๊ณต)
    • WebApplicationContext ๊ฐ์ฒด๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ src/main/webapp ํด๋”์˜ Spring Bean Configuration File ํŒŒ์ผ๋งŒ ์ œ๊ณต๋ฐ›์•„ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ
  • servlet : ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ์„œ๋ธ”๋ฆฟ(์›นํ”„๋กœ๊ทธ๋žจ)์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • servlet-name : ์„œ๋ธ”๋ฆฟ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ์‹๋ณ„์ž๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • servlet-class : ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ์˜ํ•ด ๊ฐ์ฒด๋กœ ์ƒ์„ฑ๋˜์–ด ์‹คํ–‰๋  ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • DispatcherServlet ํด๋ž˜์Šค : ์ปจํŠธ๋กค๋Ÿฌ(Controller) ์—ญํ• ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค (Front Controller)
  • servlet-mapping : ์„œ๋ธ”๋ฆฟ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ URL ์ฃผ์†Œ์˜ ํŒจํ„ด์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • servlet-name : ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ์˜ํ•ด ์‹คํ–‰๋  ์„œ๋ธ”๋ฆฟ์˜ ์‹๋ณ„์ž๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • url-pattern : ์„œ๋ธ”๋ฆฟ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ URL ์ฃผ์†Œ์˜ ํŒจํ„ด์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • Front Controller ์—ญํ• ์„ ์ œ๊ณตํ•˜๋Š” ์„œ๋ธ”๋ฆฟ์€ ํด๋ผ์ด์–ธํŠธ์˜ ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌ๋˜๋„๋ก ์„ค์ •
  • Spring Framework์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์˜ํ•ด ์ œ๊ณต๋˜๋Š” DispatcherServlet ํด๋ž˜์Šค ์‚ฌ์šฉ (Front Controller)
    • init-param : ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค์—๊ฒŒ ์‹คํ–‰์— ํ•„์š”ํ•œ ๊ฐ’์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • contextConfigLocation ์ด๋ฆ„์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(WebApplicationContext ๊ฐ์ฒด)์—๊ฒŒ Spring Bean Configuration File์˜ ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณต
    • ํ˜„์žฌ ์„ค์ •๋œ Front Controller ์—ญํ• ์˜ ๊ฐ์ฒด์—๊ฒŒ๋งŒ ์ œ๊ณตํ•œ Spring Bean์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ Spring Bean Configuration File
    • Spring Bean Configuration File์€ ์—”ํ„ฐ(Enter) ๋˜๋Š” [,] ๋ฐ [;]์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ ์ œ๊ณต ๊ฐ€๋Šฅ
    • load-on-startup : WAS ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋  ๋•Œ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ๋ฏธ๋ฆฌ ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ
    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ธ”๋ฆฟ(Front Controller)๋ฅผ ์š”์ฒญํ•˜์ง€ ์•Š์•„๋„ ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์ƒ์„ฑ
    • ์—˜๋ฆฌ๋จผํŠธ ๋‚ด์šฉ์œผ๋กœ ์„ค์ •๋œ ์ •์ˆ˜๊ฐ’์ด ์ž‘์„์ˆ˜๋ก ๋จผ์ € ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<!-- context-param : ๋ชจ๋“  ์›นํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์— ํ•„์š”ํ•œ ๊ฐ’์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
	<!-- => contextConfigLocation ์ด๋ฆ„์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(WebApplocationContext ๊ฐ์ฒด)์—๊ฒŒ
	Spring Bean Configuration File์˜ ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณต -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- ๋ชจ๋“  ์›นํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” Spring Bean๋ฅผ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ Spring Bean Configuration File -->
		<!-- => ๋ชจ๋“  Front Controller ์—ญํ™œ์„ ํ•˜๋Š” ์›นํ”„๋กœ๊ทธ๋žจ์—๊ฒŒ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด Spring Bean์œผ๋กœ ๋“ฑ๋ก  -->
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<!-- listener : WAS ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋  ๋•Œ Listener ํด๋ž˜์Šค๋ฅผ ์ œ๊ณต๋ฐ›์•„ ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
	<!-- Listener ํด๋ž˜์Šค : ServletContextListener ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ž์‹ํด๋ž˜์Šค -->
	<!-- => contextInitialized ๋ฉ”์†Œ๋“œ : Listener ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ์ž๋™ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ - ์ดˆ๊ธฐํ™” ์ž‘์—… -->
	<!-- => contextDestroyed ๋ฉ”์†Œ๋“œ : Listener ๊ฐ์ฒด ์†Œ๋ฉธ ์ „ ์ž๋™ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ - ๋งˆ๋ฌด๋ฆฌ ์ž‘์—… -->
	<listener>
		<!-- listener-class : ServletContextListener ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ž์‹ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
		<!-- => WAS ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋  ๋•Œ ContextLoaderListener ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๊ณ 
		contextInitialized ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ดˆ๊ธฐํ™” ์ž‘์—… -->
		<!-- ContextLoaderListener ํด๋ž˜์Šค์˜ contextInitialized ๋ฉ”์†Œ๋“œ์—์„œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(Spring Container)๋ฅผ
		์ƒ์„ฑํ•˜๊ณ  Spring Bean Configuration File์„ ์ œ๊ณต๋ฐ›์•„ Spring Bean์œผ๋กœ ๋“ฑ๋ก๋œ ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ -->
		<!-- Listener ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ์— ์˜ํ•ด ์ƒ์„ฑ๋œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(WebApplicationContext ๊ฐ์ฒด)๋Š”
		๋ชจ๋“  ์›น์ž์›(์„œ๋ธ”๋ฆฟ ๋ฐ ํ•„ํ„ฐ)์—๊ฒŒ ๊ณต์œ ๋˜์–ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ - Spring Bean ์ œ๊ณต  -->
		<!-- => WebApplicationContext ๊ฐ์ฒด๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ src/main/webapp ํด๋”์˜ Spring Bean 
		Configuration File ํŒŒ์ผ๋งŒ ์ œ๊ณต๋ฐ›์•„ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ -->
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- servlet : ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ์„œ๋ธ”๋ฆฟ(์›นํ”„๋กœ๊ทธ๋žจ)์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
	<servlet>
		<!-- servlet-name : ์„œ๋ธ”๋ฆฟ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ์‹๋ณ„์ž๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
		<servlet-name>mvc</servlet-name>
		<!-- servlet-class : ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ์˜ํ•ด ๊ฐ์ฒด๋กœ ์ƒ์„ฑ๋˜์–ด ์‹คํ–‰๋  ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค์„
		์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
		<!-- DispatcherServlet ํด๋ž˜์Šค : ์ปจํŠธ๋กค๋Ÿฌ(Controller) ์—ญํ™œ์„ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค - Front Controller -->
		<servlet-class>xyz.itwill09.mvc.DispatcherServlet</servlet-class>
	</servlet>
	
	<!-- servlet-mapping : ์„œ๋ธ”๋ฆฟ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ URL ์ฃผ์†Œ์˜ ํŒจํ„ด์„ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
	<servlet-mapping>
		<!-- servlet-name : ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ์˜ํ•ด ์‹คํ–‰๋  ์„œ๋ธ”๋ฆฟ์˜ ์‹๋ณ„์ž๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
		<servlet-name>mvc</servlet-name>
		<!-- url-pattern : ์„œ๋ธ”๋ฆฟ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ URL ์ฃผ์†Œ์˜ ํŒจํ„ด์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
		<!-- => Front Controller ์—ญํ™œ์„ ์ œ๊ณตํ•˜๋Š” ์„œ๋ธ”๋ฆฟ์€ ํด๋ผ์ด์–ธํŠธ์˜ ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌ๋˜๋„๋ก ์„ค์ • -->
		<url-pattern>*.itwill</url-pattern>
	</servlet-mapping>

	<servlet>
		<servlet-name>springMvc</servlet-name>
		<!-- Spring Framework์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์˜ํ•ด ์ œ๊ณต๋˜๋Š” DispatcherServlet ํด๋ž˜์Šค ์‚ฌ์šฉ - Front Controller -->
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- init-param : ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค์—๊ฒŒ ์‹คํ–‰์— ํ•„์š”ํ•œ ๊ฐ’์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
		<!-- => contextConfigLocation ์ด๋ฆ„์œผ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(WebApplicationContext ๊ฐ์ฒด)์—๊ฒŒ
		Spring Bean Configuration File์˜ ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณต -->
		<!-- => ํ˜„์žฌ ์„ค์ •๋œ Front Controller ์—ญํ™œ์˜ ๊ฐ์ฒด์—๊ฒŒ๋งŒ ์ œ๊ณตํ•œ Spring Bean์„ ๋“ฑ๋กํ•˜๊ธฐ 
		์œ„ํ•œ Spring Bean Configuration File -->
		<!-- => Spring Bean Configuration File์€ ์—”ํ„ฐ(Enter) ๋˜๋Š” [,] ๋ฐ [;]์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ ์ œ๊ณต ๊ฐ€๋Šฅ -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				/WEB-INF/spring/springMvc/mvc-context1.xml
				/WEB-INF/spring/springMvc/mvc-context2.xml
			</param-value>
		</init-param>
		<!-- load-on-startup : WAS ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋  ๋•Œ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ๋ฏธ๋ฆฌ ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์—˜๋ฆฌ๋จผํŠธ -->
		<!-- => ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ธ”๋ฆฟ(Front Controller)๋ฅผ ์š”์ฒญํ•˜์ง€ ์•Š์•„๋„ ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์ƒ์„ฑ -->
		<!-- => ์—˜๋ฆฌ๋จผํŠธ ๋‚ด์šฉ์œผ๋กœ ์„ค์ •๋œ ์ •์ˆ˜๊ฐ’์„ ์ž‘์„์ˆ˜๋ก ๋จผ์ € ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋กœ ์ƒ์„ฑ -->
		<load-on-startup>0</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>






๐Ÿ“Œ root-context.xml

  • root-context.xml : ๋ชจ๋“  Front Controller ์—ญํ• ์˜ ๊ฐ์ฒด์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋  ํด๋ž˜์Šค๋ฅผ Spring Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ Spring Bean Configuration File

    • DAO ํด๋ž˜์Šค ์ž‘์„ฑ์— ํ•„์š”ํ•œ ํด๋ž˜์Šค๋ฅผ Spring Bean์œผ๋กœ ๋“ฑ๋ก
    • DataSource, SqlSessionFactory(๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฐ์ฒด), SqlSession, TransactionManager ๋“ฑ
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	<!-- root-context.xml : ๋ชจ๋“  Front Controller ์—ญํ™œ์˜ ๊ฐ์ฒด์—์„œ ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋  ํด๋ž˜์Šค๋ฅผ
	Spring Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ Spring Bean Configuration File -->
	<!-- => DAO ํด๋ž˜์Šค ์ž‘์„ฑ์— ํ•„์š”ํ•œ ํด๋ž˜์Šค๋ฅผ Spring Bean์œผ๋กœ ๋“ฑ๋ก -->
	<!-- => DataSource, SqlSessionFactory, SqlSession, TransactionManager ๋“ฑ -->
		
</beans>





๐ŸŒผ SpringMVC

  • SpringMVC Framework : DispatcherServlet์„ ์‹œ์ž‘์œผ๋กœ ์ •์˜๋œ HandlerMapping์— ์˜ํ•ด ํ•ธ๋“ค๋Ÿฌ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ViewResolver์— ์˜ํ•ด View๋ฅผ ์ฐพ์•„ ํ™”๋ฉด์„ ๋ณด์—ฌ์คŒ

  • SpringMVC์˜ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ

  • MVC framework ๊ตฌํ˜„


๐Ÿฃ VO class (Product)

package xyz.itwill09.spring;

import lombok.AllArgsConstructor;
import lombok.Data;

@AllArgsConstructor
@Data
public class Product {
	private int num;
	private String name;
}






๐Ÿ“™ HandlerMapping.xml

  • Spring Framework ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ SimpleUrlHandlerMapping ํด๋ž˜์Šค๋ฅผ Spring Bean์œผ๋กœ ๋“ฑ๋ก

    • SimpleUrlHandlerMapping ๊ฐ์ฒด : ์ปจํŠธ๋กค๋Ÿฌ(DispatcherServlet)์—๊ฒŒ ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ „๋‹ฌ๋ฐ›์•„ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด(Spring Bean)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด
    • mappings ํ•„๋“œ์— Map ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜์—ฌ ์—”ํŠธ๋ฆฌ(Entry) ์ถ”๊ฐ€
    • ๋งตํ‚ค(MapKey)๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ(String)๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ๋งต๊ฐ’(MapValue)์€ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ Spring Bean์— ๋Œ€ํ•œ beanName(String)์„ ์ „๋‹ฌํ•˜์—ฌ Map ๊ฐ์ฒด์˜ ์—”ํŠธ๋ฆฌ๋กœ ์ถ”๊ฐ€
    • map ์—˜๋ฆฌ๋จผํŠธ ๋Œ€์‹  props ์—˜๋ฆฌ๋จผํŠธ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Spring Framework ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ Controller ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ž์‹ํด๋ž˜์Šค๋ฅผ Spring Bean์œผ๋กœ ๋“ฑ๋ก -->
	<bean class="xyz.itwill09.spring.ListController" id="listController"/>
	<bean class="xyz.itwill09.spring.ViewController" id="viewController"/>
	
	<!-- Spring Framework ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ SimpleUrlHandlerMapping ํด๋ž˜์Šค๋ฅผ Spring Bean์œผ๋กœ ๋“ฑ๋ก -->
	<!-- SimpleUrlHandlerMapping ๊ฐ์ฒด : ์ปจํŠธ๋กค๋Ÿฌ(DispatcherServlet)์—๊ฒŒ ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ 
	์ „๋‹ฌ๋ฐ›์•„ ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด(Spring Bean)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด -->
	<!-- => mappings ํ•„๋“œ์— Map ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜์—ฌ ์—”ํŠธ๋ฆฌ(Entry) ์ถ”๊ฐ€ -->
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<!-- ๋งตํ‚ค(MapKey)๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์‚ฌํ•ญ(String)๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ๋งต๊ฐ’(MapValue)์€ 
			์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค์˜ Spring Bean์— ๋Œ€ํ•œ beanName(String)์„ ์ „๋‹ฌํ•˜์—ฌ Map ๊ฐ์ฒด์˜ 
			์—”ํŠธ๋ฆฌ๋กœ ์ถ”๊ฐ€ - map ์—˜๋ฆฌ๋จผํŠธ ๋Œ€์‹  props ์—˜๋ฆฌ๋จผํŠธ ์‚ฌ์šฉ ๊ฐ€๋Šฅ -->
			<props>
				<prop key="/list.do">listController</prop>	
				<prop key="/view.do">viewController</prop>	
			</props>	
		</property>
	</bean>
</beans>





๐ŸŒธ Model1 (ListController)

  • Model ์—ญํ• ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค (์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค)

    • Spring Framework ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ Controller ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ ์ž‘์„ฑ
    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ [/list.do]์˜ URL ์ฃผ์†Œ๋กœ ์š”์ฒญํ•œ ๊ฒฝ์šฐ ์ปจํŠธ๋กค๋Ÿฌ์— ์˜ํ•ด ๋™์ž‘๋  ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค
    • ์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ : ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ์˜ํ•ด ์ปจํŠธ๋กค๋Ÿฌ์— ์ž๋™ ํ˜ธ์ถœ
    • ModelAndView ๊ฐ์ฒด์— ์‘๋‹ต ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜์—ฌ ๋ฐ˜ํ™˜
  • ์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ModelAndView ๊ฐ์ฒด์˜ ์†์„ฑ๊ฐ’์œผ๋กœ ์ €์žฅ

    • ModelAndView.addObject(String attributeName, Object attributeValue)
    • ModelAndView ๊ฐ์ฒด์— ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ์†์„ฑ๊ฐ’์œผ๋กœ ์ €์žฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ (Request Scope๋กœ request.setAttribute() ๋ฉ”์†Œ๋“œ์™€ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ ์ œ๊ณต)
    • modelAndView.addObject("productList", productList);
  • ์‘๋‹ต์— ๋Œ€ํ•œ ์ด๋™ ๊ด€๋ จ ์ •๋ณด(ViewName)์„ ModelAndView ๊ฐ์ฒด ํ•„๋“œ์— ์ €์žฅ

    • ModelAndView.setViewName(String viewName) : ๋ทฐ์ด๋ฆ„(ViewName)์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฉ”์†Œ๋“œ
    • ์ปจํŠธ๋กค๋Ÿฌ์— ์˜ํ•ด ๋ทฐ์ด๋ฆ„(ViewName)์€ JSP ๋ฌธ์„œ๋กœ ๋ณ€๊ฒฝ๋˜์–ด ํฌ์›Œ๋“œ ์ด๋™ (์‘๋‹ต)
    • modelAndView.setViewName("product_list");


package xyz.itwill09.spring;

public class ListController implements Controller {
	//์š”์ฒญ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ - ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์— ์˜ํ•ด ์ปจํŠธ๋กค๋Ÿฌ์— ์ž๋™ ํ˜ธ์ถœ
	// => ModelAndView ๊ฐ์ฒด์— ์‘๋‹ต ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜์—ฌ ๋ฐ˜ํ™˜
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//ModelAndView ๊ฐ์ฒด : ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ ๋ฐ ์‘๋‹ต์— ๋Œ€ํ•œ ์ด๋™ ๊ด€๋ จ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
		ModelAndView modelAndView=new ModelAndView();
		
		//๋ฐ์ดํƒ€ ์ฒ˜๋ฆฌ
		List<Product> productList=new ArrayList<Product>();
		productList.add(new Product(1000, "์ปดํ“จํ„ฐ"));
		productList.add(new Product(2000, "๋…ธํŠธ๋ถ"));
		productList.add(new Product(3000, "ํ•ธ๋“œํฐ"));
		
		//์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ModelAndView ๊ฐ์ฒด์˜ ์†์„ฑ๊ฐ’์œผ๋กœ ์ €์žฅ
		//ModelAndView.addObject(String attributeName, Object attributeValue) 
		// => ModelAndView ๊ฐ์ฒด์— ์ฒ˜๋ฆฌ๊ฒฐ๊ณผ๋ฅผ ์†์„ฑ๊ฐ’์œผ๋กœ ์ €์žฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ - Request Scope
		// => request.setAttribute() ๋ฉ”์†Œ๋“œ์™€ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ ์ œ๊ณต
		modelAndView.addObject("productList", productList);
		
		//์‘๋‹ต์— ๋Œ€ํ•œ ์ด๋™ ๊ด€๋ จ ์ •๋ณด(ViewName)์„ ModelAndView ๊ฐ์ฒด ํ•„๋“œ์— ์ €์žฅ
		//ModelAndView.setViewName(String viewName) : ๋ทฐ์ด๋ฆ„(ViewName)์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฉ”์†Œ๋“œ
		// => ์ปจํŠธ๋กค๋Ÿฌ์— ์˜ํ•ด ๋ทฐ์ด๋ฆ„(ViewName)์€ JSP ๋ฌธ์„œ๋กœ ๋ณ€๊ฒฝ๋˜์–ด ํฌ์›Œ๋“œ ์ด๋™ - ์‘๋‹ต
		modelAndView.setViewName("product_list");
		
		return modelAndView;
	}
}





๐ŸŒธ Model2 (ViewController)

  • Model ์—ญํ• ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค (์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค)

    • ํด๋ผ์ด์–ธํŠธ๊ฐ€ [/view.do]์˜ URL ์ฃผ์†Œ๋กœ ์š”์ฒญํ•œ ๊ฒฝ์šฐ ์ปจํŠธ๋กค๋Ÿฌ์— ์˜ํ•ด ๋™์ž‘๋  ์š”์ฒญ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค


package xyz.itwill09.spring;

public class ViewController implements Controller {
	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("product",new Product(4000,"ํ”„๋ฆฐํŠธ"));
		modelAndView.setViewName("product_view");
		return modelAndView;
	}
}





๐Ÿ“— ViewResolver.xml

  • Spring Framework ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ InternalResourceViewResolver ํด๋ž˜์Šค๋ฅผ Spring Bean์œผ๋กœ ๋“ฑ๋ก

    • prefix ํ•„๋“œ์™€ suffix ํ•„๋“œ์— JSP ๋ฌธ์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ
    • prefix ํ•„๋“œ : ๋ทฐ์ด๋ฆ„ ์•ž๋ถ€๋ถ„์— ์ถ”๊ฐ€๋  ๋ฌธ์ž์—ด (๋””๋ ‰ํ† ๋ฆฌ)
    • suffix ํ•„๋“œ : ๋ทฐ์ด๋ฆ„ ๋’ท๋ถ€๋ถ„์— ์ถ”๊ฐ€๋  ๋ฌธ์ž์—ด (ํ™•์žฅ์ž)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Spring Framework ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ InternalResourceViewResolver ํด๋ž˜์Šค๋ฅผ Spring Bean์œผ๋กœ ๋“ฑ๋ก -->
	<!-- InternalResourceViewResolver ๊ฐ์ฒด : ๋ทฐ์ด๋ฆ„(ViewName)์„ ์ „๋‹ฌ๋ฐ›์•„ JSP ๋ฌธ์„œ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ
	๋ฐ˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด -->
	<!-- => prefix ํ•„๋“œ์™€ suffix ํ•„๋“œ์— JSP ๋ฌธ์„œ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅ -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- prefix ํ•„๋“œ : ๋ทฐ์ด๋ฆ„ ์•ž๋ถ€๋ถ„์— ์ถ”๊ฐ€๋  ๋ฌธ์ž์—ด - ๋””๋ ‰ํ† ๋ฆฌ -->
		<property name="prefix" value="/WEB-INF/mvc/"/>
		<!-- suffix ํ•„๋“œ : ๋ทฐ์ด๋ฆ„ ๋’ท๋ถ€๋ถ„์— ์ถ”๊ฐ€๋  ๋ฌธ์ž์—ด - ํ™•์žฅ์ž -->
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>





๐Ÿฃ ์‘๋‹ต (product_list)

  • ์ œํ’ˆ๋ชฉ๋ก์„ ์ œ๊ณต๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” JSP ๋ฌธ์„œ

<%@ 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>SPRING</title>
</head>
<body>
	<h1>์ œํ’ˆ๋ชฉ๋ก</h1>
	<hr>
	<c:forEach var="product" items="${productList }">
		<p>์ œํ’ˆ๋ฒˆํ˜ธ = ${product.num }, ์ œํ’ˆ์ด๋ฆ„ = ${product.name }</p>
	</c:forEach>
</body>
</html>





๐Ÿฃ ์‘๋‹ต (product_view)

  • ์ œํ’ˆ์ •๋ณด๋ฅผ ์ œ๊ณต๋ฐ›์•„ ์ถœ๋ ฅํ•˜๋Š” JSP ๋ฌธ์„œ

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SPRING</title>
</head>
<body>
	<h1>์ œํ’ˆ์ •๋ณด</h1>
	<hr>
	<p>์ œํ’ˆ๋ฒˆํ˜ธ = ${product.num }, ์ œํ’ˆ์ด๋ฆ„ = ${product.name }</p>
</body>
</html>





profile
Study Log ๐Ÿ“‚

0๊ฐœ์˜ ๋Œ“๊ธ€