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

  • In 1997, the Serverlet API was introduced.

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

  • In 1999, Sun resolved this issue by introducing JavaServer Pages (JSP)

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>geeknewsservlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  <load-on-startup>1load-on-startup>
servlet>

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

<servlet-mapping>
  <servlet-name>geeknewsservlet-name>
  <url-pattern>*.htmurl-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.

 

 

 

你可能感兴趣的:(Spring MVC: Some notes)