7. MVC Patten N Spring

namibiancurrishInternet and Web Development

Nov 12, 2013 (7 years and 10 months ago)

531 views



MVC
패턴과

Spring

MVC

모델

1

MVC

모델

2
-

MVC

Controller(
또는

Action)



3?8ÿ9æ

9Û*{

%Æ9ÊC†

?ê?ò




9Û*{

%Æ9ÊC†8&

%VG²

9vHþ3‡

? BÂ
(Java Script


9Ê8ÿG¶

??9?

JSP(View)
8&3r&?

?V$û
)




9Û*{

%Æ9ÊC†+Ò

0š;Þ%?4ú

Layer


:Z%BG® †

9ZG²

/?H®

9ç8?




0š;Þ%?4ú

Layer
8–

Interface




0š;Þ%?4ú

Layer
8&3r

.r33G²

8&*B

>î,?




0š;Þ%?4ú

Layer
8&3r

.nH®&r

%Æ9ÊC†+Ò

View
8&

:Z%B

MVC

MVC

Model ?

MVC

Model

Domain Model

=

MVC

Model

Domain Model

=

+

Business Obj

+

DAO

MVC

B

R

O

W

S

E

R

JSP

모델

1

1. Request

2. Response

MVC

B

R

O

W

S

E

R

(Controller)

Servlet

(View)

JSP

모델

2

1. Request

2. Response

3. Request

4. Response

Spring Framework
-

MVC

Spring Framework
-

MVC

Spring Framework
-

MVC

모델
2
기반

프레임워크

만들기

Spring MVC

DispatcherServlet

요청

1

2

3

5

6

HandlerMapping

Controller

ModelAndView

4

ViewResolver

View

Spring MVC

Spring MVC
-

Controller

실습

1

HelloWorldController

Spring MVC

DispatcherServlet

/index.html

1

2

3

5

6

BeanNameUrlHandlerMapping

MyUrlFilenameViewController

ModelAndView


index
´

4

InternalResourceViewResolver

/WEB
-
INF/jsp/index.jsp

Spring MVC
-

Controller

Spring MVC
-

Controller

UrlFilenameViewController

=

No Model
데이터

정적인

페이지

Spring MVC
-

Controller

MultiActionController

=

DispatchAction of Struts

Action of Webwork

Spring MVC
-

Controller

ListAction

B

R

O

W

S

E

R

List.do

InsertAction

Insert.do

UpdateAction

Update.do

ViewAction

View.do

DeleteAction

Delete.do

URL : Action

=

1 : 1

단점



URL


?ê?V&v

'¢,?%:

Action(Controller)


?ê?VGÊ7ÒG¾
.




Action(Controller)
?V

?ê?V&v

'¢,?%:

3z:k

Eb9Ò8&

Action
:k/J

?ê?V




G®"î9®

URL


?ê?V&v

'¢,?%:

?ê?V&n8 7Ò

G®$ê

/Ö/Ú9Ê

,$7š<&9’*²3r

333?3‡

:VG®
.




Action(Controller)


4•9æ?V

,$7š<?4n*³

9v<?/J4n9®

8 *z9?
.




#^-Š&?

/K9÷G²

3z:k

Eb9Ò*²

9ÎGÊ

9v<?/J4n9®

8 *z9?
.

Spring MVC
-

Controller

Spring MVC
-

Controller

SampleAction

B

R

O

W

S

E

R

List.do

Insert.do

Update.do

View.do

Delete.do

URL : Action

=

N : 1

Spring MVC
-

Controller

실습

2

MultiHelloWorldController

Spring MVC
-

Controller

기존

방식

Spring MVC
-

Controller

SimpleFormController

Spring MVC
-

BeanNameHandlerMapping

<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlH
andlerMapping"/>

<bean name="/user/login.do"
class="net.javajigi.user.web.LoginFormController">


<property name="validator" ref="beanValidator" />


<property name="formView" value="/decorators/login" />


<property name="successView" value="redirect:/index.html" />


<property name="userService" ref="userService" />

</bean>


<bean name="/board/board.do"
class="net.javajigi.board.web.BoardController">


<property name="boardService" ref="boardService" />


<property name="methodNameResolver"
ref="methodNameResolver" />

</bean>

Spring MVC
-

SimpleUrlHandlerMapping

<bean id="loginFormController"
class="net.javajigi.user.web.LoginFormController">


<property name="validator" ref="beanValidator" />


<property name="formView" value="/decorators/login" />


<property name="successView" value="redirect:/index.html" />


<property name="userService" ref="userService" />

</bean>


<bean id="boardController"
class="net.javajigi.board.web.BoardController">


<property name="boardService" ref="boardService" />


<property name="methodNameResolver"
ref="methodNameResolver" />

</bean>

Spring MVC
-

SimpleUrlHandlerMapping

<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandl
erMapping">


<property name="mappings">


<props>


<prop key="/user/login.do">loginFormController</prop>



<prop key="/board/board.do">boardController</prop>


</props>


</property>

</bean>

Spring MVC
-

Interceptor

<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">


<property name="interceptors">


<list>


<ref bean="profilingInterceptor"/>


</list>


</property>



<bean id="loggingInterceptor" class="net.javajigi.web.LoggingInterceptor"/>




<property name="mappings">


<props>


<prop key="/user/login.do">loginFormController</prop>



<prop key="/board/board.do">boardController</prop>


</props>


</property>

</bean>

Spring MVC


䥮瑥牣数e潲

Spring MVC
-

View

Model

JSTLView

ExcelView

PDFView

CSVView

HTML
문서

Excel
문서

PDF
문서

CSV
문서

Spring MVC
-

ViewResolver

Model

InternalResource ViewResolver

Velocity ViewResolver

FreeMarker ViewResolver

ResourceBundleViewResolver

JSP

Velocity

Freemarker

ResourceBundle

Spring MVC
-

ViewResolver

ViewResolver Chain

<bean id="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">


<property name="order" value="1"/>


<property name="location" value="/WEB
-
INF/ajasee
-
views.xml"/>

</bean>


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">


<property name="viewClass">


<value>org.springframework.web.servlet.view.JstlView</value>


</property>


<property name="cache" value="false" />


<property name="prefix" value="/WEB
-
INF/jsp/" />


<property name="suffix" value=".jsp" />

</bean>

InternalResourceViewResolver


항상

䍨慩C


마지막

Property Editor

Property Editor ?

java.beans.PropertyEditor

Property Editor

<beans>


<bean id="builtInSample" class="PropertyEditorBean">


<property name="class">


<value>java.lang.String</value>


</property>


<property name="file">


<value>c:/test.txt</value>


</property>


<property name="locale">


<value> en
-
GB </value>


</property>


<property name="url">


<value>http://www.springframework.org</value>


</property>


<property name="properties">


<value>


name=foo


age=19


</value>


</property>


<property name="strings">


<value>rob,jan,rod,jurgen,alef</value>


</property>


<property name="bytes">


<value>Hello World</value>


</property>


</bean>

</beans>

Property Editor

import java.io.File;

import java.net.URL;

import java.util.Locale;

import java.util.Properties;


public class PropertyEditorBean {



private Class cls;



private File file;



private URL url;



private Locale locale;



private Properties properties;



private String[] strings;



private byte[] bytes;


}

Spring 2.0

Convention over Configuration

(CoC)

Spring 2.0

Controller

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />





HelloWorldController => helloworld.do




HelloWorldController.hello => helloworld/hello.do

Spring 2.0

Model





x.y.User => user




java.util.HashMap => hashMap




x.y.User[] => userList




G®"î

9Ê3?9®

x.y.User
9Î4úCŠ4ú+Ò

?V<?$ê

java.util.ArrayList => userList

Spring 2.0

View

<bean id="viewNameTranslator"

class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator" />


<bean id="viewResolver"


class="org.springframework.web.servlet.view.InternalResourceViewResolver">


<property name="prefix" value="/WEB
-
INF/jsp/" />


<property name="suffix" value=".jsp" />

</bean>





http://localhost:8080/helloworld.do

=> WEB
-
INF/jsp/helloworld.jsp




http://localhost:8080/helloworld/hello.do

=> WEB
-
INF/jsp/helloworld/hello.jsp

Spring 2.0

실습

3

MultiHelloWorldController


䍯C
기반으로

변경

Spring
설정

파일

관리

수평적

분리

Spring
설정

파일

관리

수직적

분리

Spring
설정

파일

관리

중소규모

Spring
설정

파일

관리

중대규모

참고문서
!!



RequestDispatcher


9Ê8ÿG¶

??9?

-Ž:r:f?R

GÊ??.•.ë

:


http://wiki.javajigi.net/pages/viewpage.action?pageId=77




Simplify Your Web App Development Using the Spring MVC Framework :

http://www.devx.com/Java/Article/22134/1954?pf=true





Property Editor Sample :
http://www.java2s.com/Code/Java/Spring/PropertyEditorBean.htm