Social Media
  • You are here:
  • Home »
  • Java »

parkrunPB – Spring 3 and Spring MVC – Part 1

Model-View-Controller(MVC) is a software pattern for user interfaces. It encourages clean design by separating out the three components of the Graphical User Interface (GUI) into three components – Model,
View and Controller

springmvc

It consists of 3 components –

  • Controller – This is normally Front Controller, which delegates to specific controls
  • Model – The controller updates the model
  • View – The controller then decides which view to return

The flow is sometimes easier to see in a sequence diagram –

mvcssd

Configuration

Front Controller – web.xml

This is the starting point for configuration –

  <display-name>parkrunPB</display-name>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

Here the DispatcherServlet is the Front Controller

Spring Configuration – spring-servlet.xml

This configuration is using the default name for the spring configuration file which matches the servlet name with a -servlet.xml appended – spring-servlet.xml

The key areas of this configuration are –

   <context:component-scan base-package="com.glenware.parkrunpb" />
   <context:annotation-config />
   <mvc:annotation-driven/>

   <bean id="jspViewResolver"
         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

   <bean id="messageSource"
         class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
      <property name="basename" value="classpath:messages" />
      <property name="defaultEncoding" value="UTF-8" />
   </bean>

In spring terms we’re basically –

  • Setting up annotation driven configuration on package com.glenware.parkrunpb
  • ViewResolver – check /WEB-INF/jsp for files ending in .jsp
  • Messages – check classpath for messages.properties

 
AboutController – Simple Example

The simplest controller in parkrunPB is the AboutController –

@Controller
public class AboutController {

   @RequestMapping("/about")
   public String about() {
      return "about";
   }

}

 The key attributes of this controller are –

  • @Controller – Marks the class as a Controller
  • @RequestMapping(“/about”) – Method is called when /parkrunPB/about is called
  • return “about” – This is where the view resolver kicks in an searches for the corresponding view – /WEB-INF/jsp/about.jsp

References

Images courtesy of (any issues and they will be removed/replaced) –

http://docs.spring.io/spring-framework/docs/3.0.x/reference/mvc.html

http://java4developers.com/2011/spring-mvc-basic-example-with-maven/

http://www.theserverside.com/news/1364355/Ajax-and-the-Spring-Framework-with-TIBCO-General-Interface

 

 

 

 

 

 

 

 

 

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: