Getting Started
mvc-overview.notes

Spring Web MVC Overview




An HTTP client, typically a browser, makes a request to the web server.

A controller, which is an annotated POJO class, receives the request, performs some business logic, and adds data to the model.

The model is simply an extension of a java.util.map and is used to store the data that is to be displayed to the user.

The controller also determines which view is to be displayed.

The view can be one of several technologies but is typically a JSP page which accesses the model using JSTL and JSP expression language.

The view is rendered and the results are sent back to the client.
using-lishys-notes.notes

Guide


The guide pane on the right contains information organized into sections.

Use the and buttons to move through the sections, or click an accordion tab (shown below) to go directly to a particular topic.



Code Reference


Each paragraph in the guide is usually associated with a particular piece of code. An orange arrow indicates that the code displayed in the middle pane is relevant to the guide text.



If the arrow is gray, simply click anywhere on the paragraph and the relevant code will be displayed.



Code Highlighter


The highlight feature identifies areas of the code which relate to a word or term in the guide. Hover the mouse over any underlined word and the relevant code will be highlighted.



You can disable the highlighting feature at any time by clicking on the button.

Don't forget to make the appropriate code page visible by clicking on any paragraph with a gray arrow.
SimpleController.java
package lishy.start.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SimpleController {
   
    @RequestMapping("/simple-request.html")
    public ModelAndView arbitraryMethod() {
        
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("show-message");
        modelAndView.addObject("message", "This is added to the model with the name 'message'");
        return modelAndView;
        
    }

}
show-message.jsp
<html>
  <body>
  
    <h1>Simple View</h1>
    
    <p style="color:green">${message}</p>
    
    <p>
      <a href="../index.html">back</a>
    </p>
    
  </body>
</html>
start-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <context:component-scan base-package="lishy.start.web"/>

  <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp"/>

</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID"
         version="2.5">

  <display-name>Getting Started</display-name>
  
  <servlet>
    <servlet-name>start</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>start</servlet-name>
    <url-pattern>/app/*</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
</web-app>
index.html
<html>
  <body>
  
    <h1>Getting Started</h1>
   
    <p>
      <a href="app/simple-request.html">Simple Request</a>
    </p>
    
  </body>
</html> 

Spring MVC Basics

We start with a high-level overview of Spring Web MVC.

The @Controller annotation indicates that a class is a web controller, and @RequestMapping maps web requests onto a method of the controller.

For example, this URL

http://host/root/app/simple-request.html

will be sent to arbitraryMethod() on this SimpleController class.
Controller methods can use a ModelAndViewModelAndView:gt(0) object to return the view name (which, in this application, is the name of a JSP page), and a model containing the data to be displayed by the view.

Here, arbitraryMethod() sets the view name to be show-message and adds a text string to the model with the name message""message"".

Spring renders the specified view and makes the data in the model available to it.

For a JSP page, the model attributes are added to the servlet request scope where they can be accessed by JSTL and JSP expression language.

Configuration

Spring's DispatcherServlet is configured to receive all requests which match a particular path, in this case /app/*.

DispatcherServlet is the central component of Spring MVC and is responsible for managing the entire request handling process.

Once loaded, DispatcherServlet will look in WEB-INF for a configuration file called [servlet-name]-servlet.xml, which in our case is start-servlet.xml

start-servlet.xml is typical of a minimal Spring MVC configuration file.
During initialization, Spring will register any controller classes located in the packageslishy specified by the base-package attribute of component-scan.
The methods on these controllers are now candidates to receive requests based on the paths specified in the @RequestMappings.

The viewResolver bean tells Spring how to convert a logical view name, returned by a controller, into a real JSP file.

Example

Let's see how all the components fit together.

A request is made by index.html to

http://host/root/app/simple-request.html

The web server receives the request and, using the servlet-mapping in web.xml, sends it to Spring's DispatcherServlet.

The DispatcherServlet uses information loaded from the start-servlet.xml file to identify the target method on the appropriate controller.

The controller adds data to the modeladdObject and specifies the name of the next viewsetViewName to be displayed.

Spring uses the viewResolver bean to convert this logical view name into a real JSP file.

The JSP page is displayed using the data from the model.
Home  |  Getting Started  |  Controllers  |  Views  |  Forms  |  REST  |  Testing  |  Configuration  |  MVC Walkthrough  |   lishblog  |  Email Lishy