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
It consists of 3 components –
The flow is sometimes easier to see in a sequence diagram –
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 –
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 –
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