Spring 2.0 技術手冊第七章 - Spring Web MVC 框架

  • View
    2.630

  • Download
    15

Embed Size (px)

Text of Spring 2.0 技術手冊第七章 - Spring Web MVC 框架

  • Spring Web MVC

    Web MVC

    Web

    OpenSource

    Web

    MVC

    Framework

    Spring

    Web MVC

    Web MVC

    Spring

    IoC

    Web MVC

    Spring

    Controller

    Controller

    7

  • Spring 2.0 http://openhome.cc

    72

    7.1 Spring Web MVC Spring Web Web

    MVC Spring Web Web MVC

    7.1.1 Eclipse Web Tools Platform Web Eclipse Web Tools PlatformWTP http://www.eclipse.org/webtools/downloadsWebTools Platform; All-in-one Tomcat 5.5 Servlet Container http://tomcat.apache.org/ Tomcat 5.5

    Eclipse

    WTP

    http://eclipse.cdpa.nsysu.edu.tw/technology/phoenix/demos/i

    nstall-wtp/install-wtp.html

    WTP Web

    Window/Open Perspective/Other

    J2EE Perspective

  • Chapter 7 Spring Web MVC

    73

    7.1

    J2EE Perspective

    Server WTP Tomcat

    WTP Web WTP

    ServersNew/Server

    7.2

    Server

    Tomcat 5.5New Server

    ApacheTomcat v5.5 Server

  • Spring 2.0 http://openhome.cc

    74

    7.3

    Server

    Next TomcatFinish

    Server

    7.4

    Server

    WTP WebFile/New/ProjectWeb/Dynamic Web ProjectNext Spring Web MVC

    FirstSpringMVC

  • Chapter 7 Spring Web MVC

    7

    7.5

    Web

    Finish

    spring.jar commons-logging.jar jar WEB-INF/lib

    7.6

    Web

  • Spring 2.0 http://openhome.cc

    76

    WTP Java Web

    Classpath src Web

    WebContent Export WARWeb Application ArchiveWTP Web

    7.1.2 Spring Web MVC Web MVC

    Front controller

    DispatchController

    Spring Web MVC org.spring- framework.web.servlet.DispatcherServlet

    DispatcherServlet

    Spring Web MVC

    web.xml DispatcherServlet

    FirstSpringMVC web.xml

    30

  • Chapter 7 Spring Web MVC

    77

    dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/mvc-config.xml 1

    dispatcherServlet *.do

    web.xml DispatcherServlet "dispatcher-

    Servlet" *.do

    "dispatcherServlet""contextConfigLocation" Bean "contextConfigLocation"

    DispatcherServlet ServletServlet

    -servlet.xml Bean

    mvc-config.xml

    Bean

    ...

    hello org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/mvc-config.xml,

  • Spring 2.0 http://openhome.cc

    78

    /WEB-INF/other-service.xml 1 ...

    DispatcherServletController Spring

    Web MVC org.springframework.web.servlet. mvc.ControllerController handleRequest()

    package org.springframework.web.servlet.mvc;

    import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;

    public interface Controller { ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception; }

    Controller DispatcherServlet handle-

    Request() org.springframework. web.servlet.ModelAndView ModelAndView View JSP Model View

    Spring Web MVCHelloController

    FirstSpringMVC HelloController.java package onlyfun.caterpillar;

    import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.mvc.Controller;

  • Chapter 7 Spring Web MVC

    7

    import org.springframework.web.servlet.ModelAndView;

    public class HelloController implements Controller { private String viewPage;

    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception { String user = req.getParameter("user"); return new ModelAndView(viewPage, "user", user); }

    public void setViewPage(String viewPage) { this.viewPage = viewPage; } }

    Controller"user"

    ModelAndView ModelAndView

    Model"Key" View Model

    Web MVC

    Web

    Servlet

    API

    Servlet API

    HttpServletRequest

    Spring Web MVCSpring Controller IoC

    Bean

    XML Controller

    Bean

  • Spring 2.0 http://openhome.cc

    71

    web.xml WEB-INF mvc-config.xml

    FirstSpringMVC mvc-config.xml

    DispatcherServlet HandlerMapping

    ControllerDispatcherServlet org.spring- framework.web.servlet.handler.BeanNameUrlHandlerMapping Bean "name" URL

    Controller /FirstSpringMVC/hello.do

    DispatcherServlet "hello.do" "name" "hello.do"

    Bean HelloController

    Controller ModelAndView DispatcherServlet

    ViewResolver View

    ViewResolver JSP View

  • Chapter 7 Spring Web MVC

    711

    org.springframework.web.servlet.view.InternalResourceViewResolverInternalResourceViewResolver "viewClass" org.spring- framework.web.servlet.view.InternalResourceViewServlet JSPServlet

    InternalResourceViewResolver "prefix""suffix" Model- AndView "hello"

    /WEB-INF/jsp/hello.jsp

    /WEB-INF/jsp/ hello.jsp

    FirstSpringMVC hello.jsp First Spring MVC Hello, ${user}!!

    ModelAndView Model InternalResourceView-

    Resolver InternalResourceView JSP

    Attribute JSP Expression Language

    hello.do "user"

    JSP"user" WTPRun/Run As/Run

    On ServerWTP Tomcat Web

    http://localhost:8080/FirstSpringMVC/hello.do?user=Justin

  • Spring 2.0 http://openhome.cc

    712

    7.7 FirstSpringMVC

    7.1.3 WebApplicationContext

    Servlet Spring

    org.springframework.web.context.ContextLoaderListener web.xml

    ...

    org.springframework.web.context.ContextLoaderListener ...

    ContextLoaderListener applicationContext.xml

    "contextConfig- Location"

    ...

    contextConfigLocation /WEB-INF/beans-config.xml, /WEB-INF/demo-service.xml ...

  • Chapter 7 Spring Web MVC

    713

    Servlet org.springframework.web. context.support.WebApplicationContextUtils ServletContextorg.springframework.web.context.WebApplicationContext

    WebApplicationContext ctx = WebApplicationContextUtils. getRequiredWebApplicationContext( this.getServletContext());

    WebApplicationContext ApplicationContext Spring

    Servlet Web ApplicationContext

    WebApplicationContext Bean

    Bean

    Date date = (Date) ctx.getBean("dateBean");

    Listener Servlet 2.2

    org.springframework.web.context.ContextLoaderServlet ContextLoaderListener

    ...

    contextLoader org.springframework.web.context.ContextLoaderServlet 1 ...

    Servlet

  • Spring 2.0 http://openhome.cc

    714

    WebApplicationContextDemo TimeServlet.java package onlyfun.caterpillar;

    import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context. support.WebApplicationContextUtils;

    public class TimeServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { WebApplicationContext ctx = WebApplicationContextUtils. getRequiredWebApplicationContext( this.getServletContext());

    PrintWriter out = res.getWriter(); out.println(ctx.getBean("dateBean")); } }

    Servlet WebApplicationContext

    "dateBean" web.xml ContextLoaderListener Bean

    WebApplicationContextDemo web.xml 30

  • Chapter 7 Spring Web MVC

    71

    contextConfigLocation /WEB-INF/beans-config.xml

    org.springframework.web.context.ContextLoaderListener

    time onlyfun.caterpillar.TimeServlet

    time /time.do

    "contextConfigLocation" Bean

    Bean

    WebApplicationContextDemo beans-config.xml

  • Spring 2.0 http://openhome.cc

    716

    TimeServlet

    7.8 WebApplicationContextDemo

    7.1.4 Handler Mapping DispatcherServlet Handler mapping

    Controller Web

    URL Controller

    DispatcherServlet Handler mapping org.springframework. web.servlet.handler.BeanNameUrlHandlerMapping Bean BeanNameUrlHandlerMapping

    BeanNameUrlHandlerMapping Bean "name" URL

    Controller

    ...

    ...

  • Chapter 7 Spring Web MVC

    717

    /hello.doDispatchServlet

    HelloController

    BeanNameUrlHandlerMapping Handler mapping

    URL Controller

    Spring Handler mapping org.springframework. web.servlet.handler.SimpleUrlHandlerMapping

    ...

    helloController welcomeController

    ...

    ...

  • Spring 2.0 http://openhome.cc

    718

    "mappings" "key" URL Bean "key"

    Wildcard

    ...

    helloController welcomeController ....

    hello .do URL Be