본문 바로가기
국비교육

국비교육 73일차

by Diligejy 2019. 3. 21.

1.

어제 내용 복습

SpringWeb / WebContext /WEB-INF

Step01 = 설정 파일 MVC패턴

ⓐ web.xml = <context-param> = applicationContext.xml (beans를 가장 먼저 로딩)

 <servlet> = <servlet-name>

ⓑ applicationContext.xml = beans, model을 연동하기 위한 파일

ⓒ dispatcher-servlet-xml = view



2.

MVC 설계 

ⓐ Class를 패키지별로 구현 

ⓑ Controller / Model

ⓒ Beans에 등록


- src : class

- WebContent : 가상의 디렉토리이며 하위에 url로 매핑할 수 있는 view 파일들을 관리한다.

- WebContent/META-INF : 현재 프로젝트 기본 정보, Context.xml - JNDI

- WebContent/WEB-INF : 외부에서 URL로 매핑이 불가능한 폴더 웹페이지의 정보를 기록하는 web.xml

- WebContent/WEB-INF/lib : 현재 프로젝트에 추가로 참조되는 라이브러리


* Spring Only

- WebContent/WEB-INF/폴더/view 페이지를 작성한다.

ex) step01에서 dispatcher-servlet.xml에 ViewResolver로 View페이지를 만드는 코드를 작성하고 Controller에서 ModelAndView의 mav.setViewName("hello");로 링크를 건다.


<property name = "prefix" value = "/step01/" />

mav.setViewName("hello")

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


mav.setViewName("hello") 해주면 - > WebContent/step01/hello.jsp 따옴



순서 : 

ⓐ 웹브라우저에서 /step01/hello.do로 요청한다.

ⓑ @Controller로 @RequestMapping("/step01/hello.do")를 찾는다.

ⓒ @RequestMapping이 선언된 클래스 또는 메소드를 실행한다.


3. 주요클래스

ⓐ ModelAndView  = Model(Biz+DAO) + View의 기능을 가진다.


EX01) 


/test/a로 url 요청을 하게 되면

WebContent/abc/a.jsp로 이동하게 되는 방법

ⓐ @RequestMapping("/test/a") -> test앞에 '/'없으면 무한루프발생

ⓑ mav.setViewName("a")

ⓒ <property name = "prefix" value = "/abc/" />

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


EX02)

/member/login.do를 요청하면 

/res/login.jsp로 이동하게 된다.


ⓐ @RequestMapping("/member/login.do")

ⓑ mav.setViewName("login")

ⓒ <property name = "prefix" value = "/res/" />

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



ModelAndView mav = new ModelAndView("hello"); 는 mav.setViewName("hello")와 같은 의미
ModelAndView mav = new ModelAndView("hello", "message", "연습이라네")는 mav.setViewName과 mav.addObject("message", "연습이라네")와 같은 의미

4.
org.springframework.web.servlet

Class DispatcherServlet

  • All Implemented Interfaces:
    java.io.Serializable, ServletServletConfigAwareApplicationContextAwareEnvironmentAwareEnvironmentCapable


    public class DispatcherServlet
    extends FrameworkServlet
    Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service exporters. Dispatches to registered handlers for processing a web request, providing convenient mapping and exception handling facilities.

    This servlet is very flexible: It can be used with just about any workflow, with the installation of the appropriate adapter classes. It offers the following functionality that distinguishes it from other request-driven web MVC frameworks:

    • It is based around a JavaBeans configuration mechanism.
    • It can use any HandlerMapping implementation - pre-built or provided as part of an application - to control the routing of requests to handler objects. Default isBeanNameUrlHandlerMapping and RequestMappingHandlerMapping. HandlerMapping objects can be defined as beans in the servlet's application context, implementing the HandlerMapping interface, overriding the default HandlerMapping if present. HandlerMappings can be given any bean name (they are tested by type).
    • It can use any HandlerAdapter; this allows for using any handler interface. Default adapters are HttpRequestHandlerAdapterSimpleControllerHandlerAdapter, for Spring'sHttpRequestHandler and Controller interfaces, respectively. A default RequestMappingHandlerAdapter will be registered as well. HandlerAdapter objects can be added as beans in the application context, overriding the default HandlerAdapters. Like HandlerMappings, HandlerAdapters can be given any bean name (they are tested by type).
    • The dispatcher's exception resolution strategy can be specified via a HandlerExceptionResolver, for example mapping certain exceptions to error pages. Default areExceptionHandlerExceptionResolverResponseStatusExceptionResolver, and DefaultHandlerExceptionResolver. These HandlerExceptionResolvers can be overridden through the application context. HandlerExceptionResolver can be given any bean name (they are tested by type).
    • Its view resolution strategy can be specified via a ViewResolver implementation, resolving symbolic view names into View objects. Default is InternalResourceViewResolver. ViewResolver objects can be added as beans in the application context, overriding the default ViewResolver. ViewResolvers can be given any bean name (they are tested by type).
    • If a View or view name is not supplied by the user, then the configured RequestToViewNameTranslator will translate the current request into a view name. The corresponding bean name is "viewNameTranslator"; the default is DefaultRequestToViewNameTranslator.
    • The dispatcher's strategy for resolving multipart requests is determined by a MultipartResolver implementation. Implementations for Apache Commons FileUpload and Servlet 3 are included; the typical choice is CommonsMultipartResolver. The MultipartResolver bean name is "multipartResolver"; default is none.
    • Its locale resolution strategy is determined by a LocaleResolver. Out-of-the-box implementations work via HTTP accept header, cookie, or session. The LocaleResolver bean name is "localeResolver"; default is AcceptHeaderLocaleResolver.
    • Its theme resolution strategy is determined by a ThemeResolver. Implementations for a fixed theme and for cookie and session storage are included. The ThemeResolver bean name is "themeResolver"; default is FixedThemeResolver.


5.

@Autowired
private HelloService service;는

public HelloController(HelloService service) {
this.service = service;
}
public void setHelloService (HelloService service) {
this.service = service;

,

과 같은 의미이다.


6.


Controller => Service(Biz) => Dao


7.



'국비교육' 카테고리의 다른 글

국비교육 75일차  (0) 2019.03.25
국비교육 74일차  (0) 2019.03.22
국비교육 72일차  (0) 2019.03.20
국비교육 71일차  (0) 2019.03.19
국비교육 70일차  (0) 2019.03.18

댓글