Spring MVC

์žฅ๋ฏผ์šฐยท2021๋…„ 10์›” 20์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
3/5
post-thumbnail

Spring mvc


spring mvc๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ mvcํŒจํ„ด๊ณผ ๋™์ผํ•œ ์›€์ง์ž„์„ ๋ณด์—ฌ์ค€๋‹ค.
spring mvc๋ฅผ ์‚ฌ์šฉ์„ ํ• ๋ ค๋ฉด ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์— spring webmvc-4.3.9.RELEASE.jar
ํŒŒ์ผ์„ ๋„ฃ์–ด์ค˜์•ผ์ง€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!!!


Spring Controller ์ค€๋น„

Controller

- mvc์—์„œ๋„ Controller์„ ํ†ตํ•ด ์–ด๋–ค ์ž๋ฐ” ๋นˆ ๊ฐ์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์„ Controller๊ฐ€ ์ •ํ•ด์ค€๋‹ค.
ย ย ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Spring mvc์—์„œ๋„ controller๋ฅผ ํ†ตํ•ด ์–ด๋–ค ์•ก์…˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์„ ์ •ํ•ด ์ค˜์•ผํ•œ๋‹ค.

Front Controller

ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์•ž๋‹จ์—์„œ ์„œ๋ธ”๋ฆฟ ํ•˜๋‚˜๊ฐ€ ํด๋ผ์ด์–ธํŠธ์˜ ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›๋Š”๋‹ค. ์š”์ฒญ์„ ๋ฐ›์œผ๋ฉด ๊ทธ๊ฒƒ์— ๋งž๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ฐพ์•„์„œ ํ˜ธ์ถœํ•œ๋‹ค ๊ทธ๋ฆฌ๊ณ  ํ˜ธ์ถœ์— ๋Œ€ํ•œ ๊ณตํ†ต ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ด์กŒ๊ณ , ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์ด์ œ Servlet ํด๋ž˜์Šค๊ฐ€ ์•„๋‹ˆ์–ด๋„ ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ๋ฌธ์ œ๊ฐ€ ์—†์–ด์ง„๋‹ค.

(์—ฌ๊ธฐ์„œ ์Šˆํผํด๋ž˜์Šค๋Š” DispatcherServlet๋กœ ํ•œ๋‹ค ๋ฌด์กฐ๊ฑด DispatcherServlet๋ฅผ ์ƒ์† ๋ฐ›์•„์•ผ์ง€๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค!! )
<!-- ์‚ฌ์šฉํ•  bean๊ฐ์ฒด ์ •์˜ -->
  <bean id="t1" class="ex1.Test1Action" name="/t1.inc"/>
  <!-- ๋’ค์— name์€ url mapping๋ช…์ด๋‹ค.
  	   ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋„๋ฉ”์ธ ๋’ค์— /t1.inc๋ผ๊ณ  ์š”์ฒญํ•˜๋ฉด Test1Action์ด ๊ตฌ๋™๋œ๋‹ค. -->  	
  <bean id="t2" class="ex1.Test2Action" name="/t2.inc"/>


  <!-- View Resolver์„ค์ • -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="suffix" value=".jsp"/><!-- ํ™•์žฅ์ž ์ง€์ • -->
		<property name="prefix" value="/WEB-INF/jsp/"/><!-- ์œ„์น˜ ์ง€์ • -->
		
	</bean>

โ€ข Controller-servlet.xml

ย ย ย ์ด๋ ‡๊ฒŒ controller๋ฅผ DispatcherServlet ์ƒ์†์„ ๋ฐ›์•„ ์ƒ์„ฑ์„ ํ•˜๋ฉด ์„œ๋ฒ„๋ฅผ ์‹คํ–‰์„ ํ•˜๋ฉด์„œ
ย ย ย WEB-INF/Controller-servlet.xml ๋ฅผ ๋กœ๋“œ๋ฅผ ํ• ๋ ค๊ณ  ํ•œ๋‹ค ์—ฌ๊ธฐ์— ํ•„์š”ํ•œ ๋นˆ ๊ฐ์ฒด๋“ค์„ ๋“ฑ๋ก์„ ํ•ด์ค˜์•ผ ย ย ย ํ•˜๊ณ  name : URL Mapping๋ช…์„ ์ง€์ •์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

ย ย ย (ViewResolver๋ฅผ ์ง€์ •์„ ํ•˜์—ฌ ํ™•์žฅ์ž์™€ ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์„œ ํ›จ์”ฌ ๊ฐ„ํŽธํ•˜๊ฒŒ jsp๋กœ ์ด๋™์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.)

Action Controller

Action controller์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค ์ด ์ค‘ 3๊ฐ€์ง€๋งŒ ์ •์˜ํ•ด๋ณด์ž

public class Test1Action extends AbstractController {

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse reseponse) throws Exception {
		// ํ˜„์žฌ ๊ฒ์ฒด๋Š” Controller์— ์˜ํ•ด์„œ ํ˜ธ์ถœ๋œ๋‹ค.
		// ๋‹จ, ํŠน์ • ์ด๋ฆ„์œผ๋กœ ๋“ฑ๋ก๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค!!(t1.inc)
		
		// jsp์—์„œ ํ‘œํ˜„ํ•  ์ž์›๋“ค์€ request์— ์ €์žฅํ•˜์ž!
		request.setAttribute("msg", "ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค.");
		
		// ๋ฐ˜ํ™˜๊ฐ์ฒด ์ƒ์„ฑ
		ModelAndView mv = new ModelAndView();
		
		// forwardํ•  jsp ๊ฒฝ๋กœ๋ฅผ mv์— ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
		mv.setViewName("ex1");
		
		// mv์— ํ•„์š”ํ•œ ์ •๋ณด ์ €์žฅ
		mv.addObject("str", "1000");
		
		return mv;
	}

}

โ€ข AbstractController

ย ย ย Action ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋•Œ๋Š” AbstractController ๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์†์„ ๋ฐ›๋Š”๋‹ค
ย ย ์—ฌ๊ธฐ์„œ๋Š” request๋กœ forward๋˜์„œ jsp์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œํ˜„ํ•ด์„œ ๋ณด์—ฌ์ค€๋‹ค.
ย ย ํ•˜์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ModelAndView๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ request๋กœ forward๋˜์„œ ๋„˜์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์—
ย ย ModelAndView ๊ณณ์— ์ •๋ณด๋“ค์„ ๋‹ด์•„์„œ ๋ณด๋‚ด์ฃผ๋ฉด ๋œ๋‹ค.

public class Test3Action implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		// ๋จผ์ € ํ˜„์žฌ ๋‚ ์งœ๋ฅผ ๊ตฌํ•œ๋‹ค.
		Date now = new Date(System.currentTimeMillis());
		
		// ๋ฐ˜ํ™˜ ๊ฐ์ฒด ์ƒ์„ฑ
		ModelAndView mv = new ModelAndView();
		
		mv.addObject("now", now.toString());
		mv.setViewName("ex3");
		
		
		return mv;
	}

}

โ€ข Controller

ย ย ย AbstractController ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ™์€ ์›€์ง์ž„์„ ๋ณด์—ฌ์ค€๋‹ค.
ย ย ย ๋‹จ ์ƒ์†์„ ๋ฐ›์•„ ์ •์˜ํ•˜๋Š”์ง€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”์ง€์˜ ์ฐจ์ด์  ๋ฟ ์ด๋‹ค.

โ€ข Annotation

ย ย ย  ์œ„์˜ 2๊ฐœ์˜ Action Controller๋Š” ํ•œ๊ฐœ์˜ ์š”์ฒญ๋งŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด
ย ย ย  ๋„ˆ๋ฌด ๋งŽ์€ Action Controller๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•œ ํด๋ž˜์Šค์—์„œ ์—ฌ๋Ÿฌ๊ฐ€์ง€์˜
ย ย ย ย ์š”์ฒญ์„ ๋ฐ›์•„ ์ˆ˜ํ–‰์„ ํ• ๋ ค๋ฉด Annotation์„ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ ํด๋ž˜์Šค์—์„œ
ย ย ย ย ์—ฌ๋Ÿฌ๊ฐ€์ง€์˜ ๊ฐ์ฒด๋“ค๋กœ ์›ํ•˜๋Š” ๊ฐ’๋“ค์„ forward๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค!!๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

โ€ฃ @Controller

ย ย ย ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค์— @Controller ์–ด๋…ธํ…Œ์ด์…˜์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์ด ์ ์šฉ๋œ ํด๋ž˜์Šค๋Š” ย ย ย "Controller"์ž„์„ ๋‚˜ํƒ€๋‚˜๊ณ , bean์œผ๋กœ ๋“ฑ๋ก๋˜๋ฉฐ ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ Controller๋กœ ์‚ฌ์šฉ๋จ์„ Spring ย ย ย Framework์— ์•Œ๋ฆฝ๋‹ˆ๋‹ค.

โ€ฃ @RequestMapping

ย ย ย ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์ด ์„ ์–ธ๋œ ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉ”์†Œ๋“œ๊ฐ€ ํ•˜๋‚˜์˜ ์š”์ฒญ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ• ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค. ย ย ย ์˜ˆ๋ฅผ๋“ค์–ด, "/student" ์š”์ฒญ์— ๋Œ€ํ•ด ๊ณตํ†ต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ๋  ํด๋ž˜์Šค๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์š”์ฒญ ย ย ย url์— ๋Œ€ํ•ด ํ•ด๋‹น ๋ฉ”์†Œ๋“œ์—์„œ ์ฒ˜๋ฆฌํ•ด์•ผ ๋˜๋Š” ๊ฒฝ์šฐ์—๋„ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ @RequestMapping์— ๋Œ€ํ•œ ย ย ย ๋ชจ๋“  ๋งคํ•‘ ์ •๋ณด๋Š” Spring์—์„œ ์ œ๊ณตํ•˜๋Š” HandlerMapping Class๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

annotation์„ ์‚ฌ์šฉํ•˜๋ฉด ์ „ ์ฒ˜๋Ÿผ requset.getparamter("")๋กœ ๋ฐ›์•„ ์ •๋ณด๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์ด๋ฆ„๋งŒ ๊ฐ™๋‹ค๋ฉด rquest๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”์—†์ด ํ•œ๋ฒˆ์— ๊ฐ’์ด ๋“ค์–ด์˜ค๋Š” ํŽธ๋ฆฌ์„ฑ์ด ๋„ˆ๋ฌด ์ข‹๋‹ค!!!๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

์˜ค๋Š˜์˜ ์ถ”๊ฐ€ Annotation
profile
๊ฟˆํ‹€๋Œ€๋Š”๊ผฌ๋งˆ๊ฐœ๋ฐœ์ž

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