Skip to content

Spring MVC

  • Framework for building web applications in java
  • Based on Model-View-Controller design pattern
  • Reusable UI components

Spring MVC configuration

  1. Configure Spring MVC Dispatcher Servlet
  2. Set up URL mappings to Spring MVC Dispatcher Servlets

  3. Spring dependencies (jar)

  4. commons-logging-1.2.jar
  5. javax.servlet.jsp.jstl-1.2.1.jar
  6. javax.servlet.jsp.jstl-api-1.2.1.jar
  7. Spring dependencies (maven)
  8. org.springframework.spring-context
  9. org.springframework.spring-webmvc
  10. javax.servlet.javax.servlet-api
  11. javax.servlet.jstl

MVC architecture

  • Request is sent to a controller
  • Controller ask data from the database in the format of a model
  • The data is display on a page in a form of a view

  • A front controller is the main controller which redirects traffic to other controllers

  • the front controller is defined in web.xml

  • in spring, the front controller is the dispatcher servlet. And it's configuration is defined in hvitoi-servlet.xml

Configuration files

  • web.xml
<!-- Spring MVC Configs -->
<web-app>

  <display-name>Spring MVC application</display-name>

  <!-- Configure Spring MVC Dispatcher Servlet -->
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name> <!-- A configuration file for the servlet: xxx-servlet.xml -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- This dependency must be added to maven -->
    <!-- <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </init-param> -->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Set up URL mapping for Spring MVC Dispatcher Servlet -->
  <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern> <!-- Dispatcher servlet is called whenever this pattern is called -->
  </servlet-mapping>

</web-app>
  • servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Add support for component scanning. Where to find annotations @Controller -->
    <context:component-scan base-package="com.hvitoi" />

    <!-- Add support for conversion, formatting and validation support &ndash;&gt;-->
    <mvc:annotation-driven />

    <!-- Include resources (images, css files, etc) -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Custom message resources -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames" value="/resources/properties/messages.properties" />
    </bean>

    <!-- Define Spring MVC view resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>
  • Controller
@Controller
@RequestMapping("/hello") // parent mapping
public class HelloController {

    // method to show initial html form
    @RequestMapping("/showForm") // /hello/showForm
    public String showForm() {
        return "form";
    }

    // method to process the html form
    @RequestMapping("/greeting")
    public String processForm() {
        return "greeting";
    }

    // method to read form data
    @RequestMapping("/yoGreeting")
    public String shoutName(HttpServletRequest request, Model model) {

        String theName = request.getParameter("studentName"); // GET params
        theName = theName.toUpperCase();
        String result = "Yo! " + theName;

        // add new attribute to the model
        model.addAttribute("message", result);

        return "greeting";
    }

    @RequestMapping("/heyGreeting")
    public String shoutNameDifferently(
            @RequestParam("studentName") String theName, // take the name from the get params
            Model model) {

        theName = theName.toUpperCase();
        String result = "Hey! " + theName;

        // add new attribute to the model
        model.addAttribute("message", result);

        return "greeting";
    }
}