Spring MVC
Spring MVC
It’s Time
Dror Bereznitsky
Senior Consultant and
Architect, AlphaCSP
Introduction
Background
Features Review
– Configuration
– View technology
– Page flow
– Table sorting
– Search results pagination
– Validation
– AJAX
– Error handling
– I18n
– Documentation
Summary
Simple model
– Easy to get going - fast learning curve
Designed for extension
– Smart extension points put you in control
Strong integration
– Integrates popular view technologies
A part of the Spring framework
– All artifacts are testable and benefit from
dependency injection
Introduction
Background
Features Review
– Configuration
– View technology
– Page flow
– Table sorting
– Search results pagination
– Validation
– AJAX
– Error handling
– I18n
– Documentation
Summary
Handler
Introduction
Background
Features Review
– Configuration
– View technology
– Page flow
– Table sorting
– Search results pagination
– Validation
– AJAX
– Error handling
– I18n
– Documentation
Summary
/WEB-INF/spring-mvc-demo.xml
– Setting up the dispatcher for annotation support
– Actually done by default for DispatcherServlet
<bean class=
"org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class=
"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<context:component-scan base-package="com.alphacsp.webFrameworksPlayoff"/>
View Resolvers
– Renders the model
– Decoupling the view technology
JSTL view
– JSP + JSTL –native choice for Spring MVC
– Spring tag libraries
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view. JstlView “ />
<property name="prefix" value="/WEB-INF/views"/>
<property name="suffix" value=".jsp"/>
</bean>
1
Handle GET
/demo/phoneBook
DispatcherServlet PhoneBookController
phoneBook
2 phoneBook WEB-INF/views/phoneBook.jsp/
InternalResource
ViewResolver
1
Handle GET
/demo/phoneBook
DispatcherServlet PhoneBookController
phoneBook
2 phoneBook WEB-INF/views/phoneBook.jsp/
InternalResource
ViewResolver
Sorting by column
@ModelAttribute annotations
@RequestMapping(value = "/phoneBook/list")
protected ModelAndView onSubmit(
@ModelAttribute("contact") Contact contact, BindingResult result)
<bean id="configurationLoader"
class="DefaultXmlBeanValidationConfigurationLoader">
1
<property name="resource" value="WEB-INF/validation.xml" />
</bean>
<bean id="beanValidator"
class="org.springmodules.validation.bean.BeanValidator">
2
<property name="configurationLoader" ref="configurationLoader" />
</bean>
</validation> Validation.xml
@RequestMapping(value = "/phoneBook/list")
protected ModelAndView onSubmit(@ModelAttribute("contact") Contact
contact, BindingResult result) throws Exception {
…
validator.validate(contact, result);
… PhoneBookController.java
Valang validator
– validation is defined in valang
expression language
<bean id="clientSideValidator"
class="org.springmodules.validation.valang.ValangValidator">
<property name="valang">
<value>
<![CDATA[
{ firstName : ? IS NOT BLANK OR department IS NOT BLANK
OR email IS NOT BLANK : 'At least one field is required'}
]]>
</value>
</property>
</bean>
<td class="search">
<form:input id="department" path="department" tabindex="3"
cssClass="searchField"/>
<div id="departmentList" class="auto_complete"></div>
<script type="text/javascript">
</script>
</td>
phoneBook.jsp
HandlerExceptionResolvers - handle
unexpected exceptions
– Programmatic exception handling
– Information about what handler was
executing when the exception was
thrown
– SimpleMappingExceptionResolver – map
exception classes to views
LocaleResolver
– automatically resolve messages
using the client's locale
• AcceptHeaderLocaleResolver
• CookieLocaleResolver
• SessionLocaleResolver
LocaleChangeInterceptor
– change the locale in specific cases
Reloadable resource bundles
Introduction
Background
Features Review
– Configuration
– View technology
– Page flow
– Table sorting
– Search results pagination
– Validation
– AJAX
– Error handling
– I18n
– Documentation
Summary
Copyright AlphaCSP Israel 2008 – Web Framework Playoff Seminar
42
Summary:: Pros
Pros:
– Highly flexible
– Strong REST foundations
– A wide choice of view technologies
– New annotation configuration,
less XML, more defaults
– Integrates with many common web
solutions
– Easy adoption for Struts 1 users
Cons:
– Model2 MVC forces you to build
your application around
request/response principles
as dictated by HTTP
This was done by design in Spring MVC
– Requires a lot of work in the
presentation layer: JSP, Javascript, etc.
– No AJAX support out of the box
– No components support
http://springframework.org/
Spring Source
Spring Modules
Spring IDE
Appfuse light - spring MVC quickstarts
Matt Raible - Spring MVC
Expert Spring MVC and Web Flow