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

  • Published on
    18-Jul-2015

  • View
    2.628

  • Download
    15

Embed Size (px)

Transcript

<ul><li><p>Spring Web MVC </p><p>Web MVC</p><p>Web</p><p>OpenSource</p><p>Web </p><p>MVC</p><p>Framework</p><p>Spring</p><p>Web MVC</p><p>Web MVC</p><p>Spring</p><p>IoC</p><p>Web MVC</p><p>Spring</p><p>Controller</p><p>Controller</p><p>7 </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>72 </p><p>7.1 Spring Web MVC Spring Web Web </p><p>MVC Spring Web Web MVC </p><p>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 </p><p>Eclipse</p><p>WTP</p><p>http://eclipse.cdpa.nsysu.edu.tw/technology/phoenix/demos/i</p><p>nstall-wtp/install-wtp.html </p><p> WTP Web</p><p>Window/Open Perspective/Other</p><p>J2EE Perspective </p></li><li><p>Chapter 7 Spring Web MVC </p><p>73 </p><p>7.1 </p><p>J2EE Perspective </p><p> Server WTP Tomcat</p><p> WTP Web WTP </p><p>ServersNew/Server </p><p>7.2 </p><p>Server</p><p> Tomcat 5.5New Server</p><p>ApacheTomcat v5.5 Server </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>74 </p><p>7.3 </p><p>Server</p><p>Next TomcatFinish</p><p> Server </p><p>7.4 </p><p>Server</p><p> WTP WebFile/New/ProjectWeb/Dynamic Web ProjectNext Spring Web MVC </p><p>FirstSpringMVC </p></li><li><p>Chapter 7 Spring Web MVC </p><p>7 </p><p>7.5 </p><p>Web</p><p>Finish</p><p> spring.jar commons-logging.jar jar WEB-INF/lib </p><p>7.6 </p><p>Web</p></li><li><p>Spring 2.0 http://openhome.cc </p><p>76 </p><p> WTP Java Web </p><p>Classpath src Web </p><p> WebContent Export WARWeb Application ArchiveWTP Web</p><p>7.1.2 Spring Web MVC Web MVC</p><p>Front controller</p><p>DispatchController</p><p> Spring Web MVC org.spring- framework.web.servlet.DispatcherServlet</p><p>DispatcherServlet </p><p> Spring Web MVC</p><p> web.xml DispatcherServlet </p><p> FirstSpringMVC web.xml </p><p> 30 </p></li><li><p>Chapter 7 Spring Web MVC </p><p>77 </p><p> dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/mvc-config.xml 1 </p><p> dispatcherServlet *.do </p><p> web.xml DispatcherServlet "dispatcher- </p><p>Servlet" *.do </p><p>"dispatcherServlet""contextConfigLocation" Bean "contextConfigLocation" </p><p> DispatcherServlet ServletServlet</p><p> -servlet.xml Bean </p><p>mvc-config.xml </p><p> Bean </p><p>... </p><p> hello org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/mvc-config.xml, </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>78 </p><p> /WEB-INF/other-service.xml 1 ... </p><p>DispatcherServletController Spring </p><p>Web MVC org.springframework.web.servlet. mvc.ControllerController handleRequest() </p><p>package org.springframework.web.servlet.mvc; </p><p>import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; </p><p>public interface Controller { ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception; } </p><p> Controller DispatcherServlet handle- </p><p>Request() org.springframework. web.servlet.ModelAndView ModelAndView View JSP Model View </p><p> Spring Web MVCHelloController</p><p> FirstSpringMVC HelloController.java package onlyfun.caterpillar; </p><p>import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.mvc.Controller; </p></li><li><p>Chapter 7 Spring Web MVC </p><p>7 </p><p>import org.springframework.web.servlet.ModelAndView; </p><p>public class HelloController implements Controller { private String viewPage; </p><p> public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception { String user = req.getParameter("user"); return new ModelAndView(viewPage, "user", user); } </p><p> public void setViewPage(String viewPage) { this.viewPage = viewPage; } } </p><p> Controller"user"</p><p>ModelAndView ModelAndView</p><p>Model"Key" View Model</p><p>Web MVC</p><p>Web</p><p>Servlet</p><p>API</p><p>Servlet API</p><p>HttpServletRequest</p><p> Spring Web MVCSpring Controller IoC</p><p> Bean </p><p> XML Controller</p><p> Bean </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>71 </p><p> web.xml WEB-INF mvc-config.xml</p><p> FirstSpringMVC mvc-config.xml </p><p> DispatcherServlet HandlerMapping </p><p> ControllerDispatcherServlet org.spring- framework.web.servlet.handler.BeanNameUrlHandlerMapping Bean "name" URL</p><p> Controller /FirstSpringMVC/hello.do</p><p>DispatcherServlet "hello.do" "name" "hello.do" </p><p> Bean HelloController </p><p> Controller ModelAndView DispatcherServlet </p><p>ViewResolver View </p><p>ViewResolver JSP View</p></li><li><p>Chapter 7 Spring Web MVC </p><p>711 </p><p>org.springframework.web.servlet.view.InternalResourceViewResolverInternalResourceViewResolver "viewClass" org.spring- framework.web.servlet.view.InternalResourceViewServlet JSPServlet </p><p>InternalResourceViewResolver "prefix""suffix" Model- AndView "hello" </p><p> /WEB-INF/jsp/hello.jsp </p><p> /WEB-INF/jsp/ hello.jsp </p><p> FirstSpringMVC hello.jsp First Spring MVC Hello, ${user}!! </p><p> ModelAndView Model InternalResourceView- </p><p>Resolver InternalResourceView JSP</p><p>Attribute JSP Expression Language</p><p> hello.do "user"</p><p> JSP"user" WTPRun/Run As/Run </p><p>On ServerWTP Tomcat Web</p><p> http://localhost:8080/FirstSpringMVC/hello.do?user=Justin</p></li><li><p>Spring 2.0 http://openhome.cc </p><p>712 </p><p>7.7 FirstSpringMVC</p><p>7.1.3 WebApplicationContext </p><p> Servlet Spring </p><p> org.springframework.web.context.ContextLoaderListener web.xml </p><p>... </p><p> org.springframework.web.context.ContextLoaderListener ... </p><p>ContextLoaderListener applicationContext.xml</p><p> "contextConfig- Location" </p><p>... </p><p> contextConfigLocation /WEB-INF/beans-config.xml, /WEB-INF/demo-service.xml ... </p></li><li><p>Chapter 7 Spring Web MVC </p><p>713 </p><p> Servlet org.springframework.web. context.support.WebApplicationContextUtils ServletContextorg.springframework.web.context.WebApplicationContext </p><p>WebApplicationContext ctx = WebApplicationContextUtils. getRequiredWebApplicationContext( this.getServletContext()); </p><p>WebApplicationContext ApplicationContext Spring</p><p> Servlet Web ApplicationContext </p><p> WebApplicationContext Bean</p><p>Bean </p><p>Date date = (Date) ctx.getBean("dateBean"); </p><p> Listener Servlet 2.2</p><p> org.springframework.web.context.ContextLoaderServlet ContextLoaderListener </p><p>... </p><p> contextLoader org.springframework.web.context.ContextLoaderServlet 1 ... </p><p> Servlet </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>714 </p><p> WebApplicationContextDemo TimeServlet.java package onlyfun.caterpillar; </p><p>import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context. support.WebApplicationContextUtils; </p><p>public class TimeServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { WebApplicationContext ctx = WebApplicationContextUtils. getRequiredWebApplicationContext( this.getServletContext()); </p><p> PrintWriter out = res.getWriter(); out.println(ctx.getBean("dateBean")); } } </p><p> Servlet WebApplicationContext </p><p>"dateBean" web.xml ContextLoaderListener Bean</p><p> WebApplicationContextDemo web.xml 30 </p></li><li><p>Chapter 7 Spring Web MVC </p><p>71 </p><p> contextConfigLocation /WEB-INF/beans-config.xml </p><p> org.springframework.web.context.ContextLoaderListener </p><p> time onlyfun.caterpillar.TimeServlet </p><p> time /time.do </p><p> "contextConfigLocation" Bean</p><p>Bean </p><p> WebApplicationContextDemo beans-config.xml </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>716 </p><p> TimeServlet</p><p>7.8 WebApplicationContextDemo</p><p>7.1.4 Handler Mapping DispatcherServlet Handler mapping</p><p> Controller Web</p><p> URL Controller </p><p>DispatcherServlet Handler mapping org.springframework. web.servlet.handler.BeanNameUrlHandlerMapping Bean BeanNameUrlHandlerMapping</p><p>BeanNameUrlHandlerMapping Bean "name" URL</p><p> Controller </p><p>... </p><p> ... </p></li><li><p>Chapter 7 Spring Web MVC </p><p>717 </p><p> /hello.doDispatchServlet</p><p> HelloController </p><p>BeanNameUrlHandlerMapping Handler mapping</p><p> URL Controller </p><p> Spring Handler mapping org.springframework. web.servlet.handler.SimpleUrlHandlerMapping</p><p>... </p><p> helloController welcomeController </p><p> ... </p><p> ... </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>718 </p><p> "mappings" "key" URL Bean "key" </p><p> Wildcard </p><p>... </p><p> helloController welcomeController .... </p><p> hello .do URL Bean</p><p>"helloController" </p><p> Controller Spring 2.0 </p><p>org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping FirstSpringMVCmvc-config.xml ... </p></li><li><p>Chapter 7 Spring Web MVC </p><p>71 </p><p> ... </p><p>ControllerClassNameHandlerMapping hello* </p><p>Controller hello.do Hello- </p><p>Controller welcome* Controller- </p><p>ClassNameHandlerMapping WelcomeController </p><p>7.1.5 Handler Interceptor </p><p> Controller View</p><p> org.springframework.web.servlet.Hand- lerInterceptor </p><p>package org.springframework.web.servlet; </p><p>import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; </p><p>public interface HandlerInterceptor { boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception; void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception; } </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>72 </p><p>preHandler() Controller boolean Handler Interceptor Controller </p><p>false Interceptor ControllerpostHandler() Controller afterCompletion() View </p><p> org.springframework.web.servlet.handler.HandlerInter- ceptorAdapter HandlerInterceptor FirstSpringMVC Logging- </p><p>Interceptor </p><p> HandlerInterceptorDemo LoggingInterceptor.java package onlyfun.caterpillar; </p><p>import java.util.logging.Logger; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet. handler.HandlerInterceptorAdapter; </p><p>public class LoggingInterceptor extends HandlerInterceptorAdapter { private Logger logger = Logger.getLogger(this.getClass().getName()); </p><p> public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.info( handler.getClass().getName() + " </p><p>..."); return true; } </p><p> public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, </p></li><li><p>Chapter 7 Spring Web MVC </p><p>721 </p><p> ModelAndView modelAndView) throws Exception { logger.info( handler.getClass().getName() + " </p><p>..."); } </p><p> public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { logger.info("</p><p>..."); } } </p><p> HandlerInterceptor Bean</p><p> HandlerInterceptorDemo mvc-config.xml </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>722 </p><p> helloController </p><p> UrlHandlerMapping SimpleUrlHandlerMapping</p><p> DispatcherServlet</p><p>Console </p><p>7.9 HandlerInterceptorDemo</p><p>7.1.6 Controller Controller</p><p> Controller SpringController Spring IoC</p></li><li><p>Chapter 7 Spring Web MVC </p><p>723 </p><p> BeanSpring Controller</p><p> Controller </p><p> Spring Controller </p><p>7.10 Spring</p><p>Controller</p><p> Controller </p><p> AbstractController </p><p>AbstractController</p><p>Controller</p><p>WebContentGene- </p><p>rator</p><p>Session</p><p>synchronized</p><p>AbstractController</p><p>handleRequestInternal() </p><p>public class SomeController extends AbstractController { public ModelAndView handleRequestInternal( HttpServletRequest request, HttpServletResponse response) throws Exception { .... </p><p> return new ModelAndView("view", "modelName", model); </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>724 </p><p> } .. </p><p>} </p><p> MultiActionController </p><p>Controller</p><p>Controller</p><p>MultiActionController</p><p> BaseCommandController</p><p>AbstractCommandController </p><p>Command</p><p>BaseCommandController</p><p>Spring</p><p>Command</p><p>JavaBean</p><p>BaseCommandController </p><p>Command</p><p>AbstractCommandController</p><p>AbstractCommandController</p><p>CommandController</p><p>public class SomeCommandController extends AbstractCommandController { public SomeCommandController() { setCommandClass(YourCommand.class); } </p><p> public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException exception) throws Exception { YourCommand your = (YourCommand) command; ... </p><p> return new ModelAndView(..); } .. </p><p>} </p><p> AbstractFormController </p></li><li><p>Chapter 7 Spring Web MVC </p><p>72 </p><p>AbstractFormController</p><p> SimpleFormController </p><p>SimpleFormController</p><p>doSubmitAction() </p><p>ModelAndView</p><p>onSubmit() </p><p> AbstractWizardFormController </p><p>Abstract- </p><p>WizardFormController</p><p>Wizard</p><p> Controller Controller</p><p> Spring Web MVC</p><p> API </p><p>7.1.7 ModelAndView </p><p>ModelAndView Spring Web MVC</p><p> Model View Java</p><p> ModelAndView</p><p> Model View </p><p> ModelAndView View View </p><p>View resolver org.springframework.web.servlet.View InternalResourceView JstlView Model- </p><p>AndView </p><p>ModelAndView(String viewName) </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>726 </p><p> Model Map</p><p> ModelAndView</p><p> ModelAndView </p><p>ModelAndView(String viewName, Map model) </p><p>Map"Key""Value"</p><p> JSP Model</p><p> Model ModelAndView </p><p>ModelAndView(String viewName, String modelName, Object modelObject) </p><p> modelName View Model </p><p> View JSPPdf</p><p>ModelAndView View </p><p>ModelAndView(View view) ModelAndView(View view, Map model) ModelAndView(View view, String modelName, Object modelObject) </p><p> View org.springframework.web.servlet.view. RedirectViewModelAndView Forward RedirectViewRedirect</p></li><li><p>Chapter 7 Spring Web MVC </p><p>727 </p><p>... </p><p>public ModelAndView handleRequest(....) ... { ... </p><p> return new ModelAndView( new RedirectView(this.getViewPage())); } ... </p><p>viewPage Web</p><p> getViewPage() /FirstSpring- MVC/pages/index.htm FirstSpringMVC Web </p><p>Forward</p><p> Web Web </p><p> WEB-INF </p><p>WEB-INF </p><p>DispatcherServlet Controller </p><p> Web Web </p><p>RedirectWeb </p><p> Web WEB-INF</p><p> Web </p><p> Spring 2.0ModelAndView addObject() ModeladdObject()"" </p><p>... </p><p> public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception { </p></li><li><p>Spring 2.0 http://openhome.cc </p><p>728 </p><p> User user = new User(); ... </p><p> ModelAndView modelAndView = new ModelAndView(viewPage); modelAndView.addObject(user); return modelAndView; } ... </p><p> addOb...</p></li></ul>