zoukankan      html  css  js  c++  java
  • Spring MVC: Some notes

    The following notes are based on the article "Mastering Spring MVC", which is, though, a little bit old, but very helpful for beginners, like me. 

    The MVC Framework landscape

    Besides Spring MVC, there are also some other frameworks, like Structs, Wicket, JavaSerer Faces(JSF), Seam, etc.

    A breif history about Java Web Applications

    With servlets, dynamically constructing HTML documents became easy. But because the documents were constructed inside Java source code, maintenance was challenging.

    Rather than implementing presentation code inside servlets, JSPs let you build HTML-like documents that interweave snippets of Java code to support dynamic content. At runtime, JSPs are translated to servlet source code, compiled, and then deployed to a Web container dynamically.

    This architecture was called page-driven or Model 1. 

    In page-driven applications, each page not only contains the business logic required to generate the dynamic content, but also the control logic  to determine application flow.

    • The solution was to separate programmatic responsibilities into the technologies best suited for them.

    Thus was born the Model 2 architecture, which adheres to the MVC Smalltalk design pattern:

    1. The model represents your data or the objects with which your application is interacting. (POJO, etc)
    2. The view is a visualization of your model. (JSP, etc)
    3. The controller manages application flow and make calls into business objects. (Servlet, etc)

    In a Spring MVC architecture, you implement controllers that make calls into Spring service beans to perform business actions and then send a model object to one of your views for presentation. 

    Spring: An overview

    Key benifit of Spring is dependency injection, which is a desin patten coined by Martin Fowler, that separates an application's dependencies (and dependency configuration) from the code that uses those dependencies. 

    Introducing Spring MVC

    Spring provides a front controller servlet named DispatcherServlet. To build an application, you construct the following components:

    • One or more controllers that invoke business logic and create a ModelAndView object
    • A visualization components such as a JSP
    • XML or annotation configuration to wire the compoments together.

     Spring provides various controllers to use as base classes for creating your own controllers, like

    • Redirect to static views
    • Provide basic servlet-like functionality
    • Process commands
    • Process shared actions
    • Handle forms
    • Provide wizard-like functionality to process multipage forms

    The primary entry point for a Spring application is the DispatcherServlet, so the first step is create a DispatcherServlet in the web.xml file:

    <servlet>
      <servlet-name>geeknews</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>

    Next, the DispatcherServlet needs to be mapped to a URI pattern.

    <servlet-mapping>
      <servlet-name>geeknews</servlet-name>
      <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    There are some URL mapping beans to translate a URL pattern to a controller,

    • BeanNameUrlHandlerMapping: Maps a URL to a bean based on the name of the controller's bean, as defined in the bean's XML definition.
    • SimpleUrlHandlerMapping: Maps a URL to a bean based on a list of properties. (In Listing 2, the SimpleUrlHandlerMapping class is used to map /home.htm to the bean with the ID of homePageController.)
    • ControllerClassNameHandlerMapping: Maps a URL to a bean based on the bean's class name. For example, HomePageController would be mapped to /homePage*, such as /home.htm.
    • ControllerBeanNameHandlerMapping: Similar to the BeanNameUrlHandlerMapping mapper, but does not expect bean names to follow the URL convention. Also supports Controller annotations.
    • CommonsPathMapHandlerMapping: Maps URLs to a controller based on Jakarta Commons Attributes metadata.
    • DefaultAnnotationHandlerMapping: Maps URLs to a controller for methods that implement the RequestMapping annotation.

     




    --------------------------------------
    Regards,
    FangwenYu
  • 相关阅读:
    xcode6创建工程时 默认去掉了PrefixHeader.pch
    KVC访问私有成员
    Apple Watch 中Context Menu的应用
    Apple Watch应用创建
    NSURLConnection加载数据并展示
    UIView 的exclusiveTouch clipsToBounds和transform属性
    Shell的一些基本用法
    NS_ENUM和NS_OPTIONS
    iOS国际化时遇到错误: the data couldn't be read because it isn't in the correct format.
    iOS8中UIAlertController的使用
  • 原文地址:https://www.cnblogs.com/fangwenyu/p/2717594.html
Copyright © 2011-2022 走看看