Spring MVC is one of the most commonly used Spring Modules. At its core is the DispatcherServlet.
The DispatcherServlet offers a range of features including –
The DispatcherServlet binds to WebApplicationContext, and special beans –
DispatcherServlet Configuration
[sourcecode lang=”xml”] <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>
[/sourcecode]
By default this will look for a configuration under /WEB-INF/spring-servlet.xml
You can also use ContextLoaderListener to load multiple configuration files
[sourcecode lang=”xml”] <listener>Allows you to specify multiple xml files –
[sourcecode lang=”xml”] <context-param>Configuration
Like all Spring configurations you have the option of either –
Im not going to cover the XML configuration beyond configuration of annotation driven context, as its covered very well in the Spring documentation.
Preconfiguration
The servlet configuration above will serve all requests. The starting point is to separate requests for resources from spring MVC configurations –
[sourcecode lang=”xml”] <mvc:resources mapping="/assets/**" location="classpath:/META-INF/resources/webjars/"/>We’re using DefaultAnnotationHandlerMapping. This needs to be configured by –
[sourcecode lang=”xml”] <context:component-scan base-package="com.glenware.parkrunpb" />Other options are BeanNameUrlHandlerMapping, ControllerBeanNameHandlerMapping, ControllerClassNameHandlerMapping and SimpleUrlHandlerMapping
Finally we configure the ViewResolver –
[sourcecode lang=”xml”] <bean id="jspViewResolver"This will serve JSTL pages of the pattern –
prefix/<controllerRequest>.suffix –> /WEB-INF/jsp/<controllerRequest>.jsp
Other options for ViewResolvers include BeanNameViewResolver, TilesViewResolver and ContentNegotiatingViewResolver
Finally we should configure the messages –
[sourcecode lang=”xml”] <bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
Configuration of Controllers
The most basic controller is
[sourcecode lang=”java”] package com.glenware.parkrunpb.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class AboutController {
@RequestMapping("/about")
public String about() {
return "about";s
}
}
[/sourcecode]
Lets break this down –
1. @Controller – This annotation instructs the Spring container that the object is a Controller
2. @RequestMapping – Signifies the use of DefaultAnnotationHandlerMapping. In this instance it will serve requests for /about
The @RequestMapping can also be used at class level – eg
[sourcecode lang=”java”]@Controller
@RequestMapping("api")
public class JSONController
3. Finally we see the about method which simply forwards to “about”. This is combined with the ViewResolver –
prefix/<controllerRequest>.suffix –> /WEB-INF/jsp/about.jsp
Forms
Any real app would involve some transfer of data from the server.
An example form would be of the structure –
[sourcecode lang=”html”]<form:form method="post" action="addAdmin.html" commandName="prCourse" role="form" class="form-horizontal">
<form:errors path="*" cssClass="errorblock" element="div" />
<div class="form-group">
<label class="col-sm-2 control-label" for="prName">Course Name</label>
<div class="col-sm-4">
<form:input type="text" path="prName" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="url">Link</label>
<div class="col-sm-4">
<form:input type="text" path="url" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="averageTime">Average Time(s)</label>
<div class="col-sm-4">
<form:input type="text" path="averageTime" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="regionId">Course</label>
<div class="col-sm-4">
<form:select path="regionId" class="form-control">
<form:options items="${prRegionMap}" />
</form:select>
</div>
</div>
<div class="form-group">
<div class="col-sm-2"></div>
<div>
<input type="submit"
value="<spring:message code="label.addparkrun"/>"
class="btn btn-primary btn-lg" />
</div>
</div>
</form:form>
[/sourcecode]The key points are –
The mapping and method for processing this object is –
[sourcecode lang=”java”]@RequestMapping(value = "/addAdmin", method = RequestMethod.POST)
public String addPRCourse( @Valid @ModelAttribute("prCourse") PRCourse prCourse,
BindingResult result,
Map<String, Object> map ) {
if ( result.hasErrors() ) {
List<PRCourse> prCourseList = parkrunService.listPRCourse();
map.put("prCourseList", prCourseList);
List<PRRegion> prRegionList = parkrunService.listPRRegion();
Map <Integer, String> prRegionMap = new LinkedHashMap<Integer, String> ();
for ( PRRegion prRegion : prRegionList ) {
prRegionMap.put(prRegion.getId(), prRegion.getRegionName());
}
map.put("prRegionMap", prRegionMap);
return "admin";
} else {
prCourse.setPrregion( parkrunService.getPRRegion( prCourse.getRegionId() ) );
parkrunService.addPRCourse(prCourse);
}
return "redirect:/admin";
}
[/sourcecode]
The key points are –
Passing Single Parameters
@RequestParam
A RequestParam represents a link would be of the form –
http://localhost:8080/mysite/requestParamExample?id=123
[sourcecode lang=”java”] @RequestMapping(value = "/requestParamExample", method = RequestMethod.GET)
public String requestParamExample(@RequestParam("id") String id, Model model) {
…
return "requestParamExample";
}
@PathVariable
A RequestParam represents a link would be of the form –
http://localhost:8080/mysite/pathVariableExample/id/123
[sourcecode lang=”java”] @RequestMapping(value = "/pathVariableExample", method = RequestMethod.GET)File Upload
File Upload is covered here –
http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch16s08.html
JSON
You can also use Spring MVC to serve JSON, when combined with the Jackson libraries and the @ResponseBody annotation –
[sourcecode lang=”java”] @ControllerReferences
http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/pt03.html