2. Spring MVC
• git clone
git://github.com/SpringSource/spring-mvc-
showcase.git
• The following has been implemented in code.
– Ajax Controller
– Mapping Request
– Obtaining Request Data and Generating Response
– Rendering Views
– Validation
– Exception Handling
4. Framework Model
• Spring MVC is request based framework, which means a single front controller servlet will delegate
to action models based on request URL/params while in component based MVC, a single front
controller will gather, convert and validate request parameters or update the model values before
invoke the business action.
• Asyn request implementation of MVC model defines CallableController where a callable() method
is implemented to return data and throws a checked exception.
– In Asyn request, if it times out,
• Sync request implementation of MVC model can be achieved by defining a SimpleController using
@RestController annotation.
• When response data needs to be rendered, a ViewsController is defined to prepare the Model data
and return a path for viewed jsp. A view rendering will define the following classes for various
approach.
– DefaultRequestToViewNameTranslator convention
– Using path variables in a view template
– Data binding with URI variables
– HTML generated by JSP template
5. MVC Implementation
• In CallableController class, a few methods need to be implemented:
– callableWithException() with request parameters.
– handleException() intercepts ExceptionHandler.
• For controller specific exception handling, define ExceptionController as a type of RestController and
implement handle() method with input of specific exception.
• Custom exception handling can be implemented using @RestControllerAdvise and @ExceptionHandler
and implement handle%SpecificException, for example,
handleNotFoundException(CustomNotFoundException e) with ResponseMsg.
• Based on path patterns, request is mapped and produced in specific mediaType such as XML or JSON.
• Redirecting
– URL redirect allows one web page to be accessed via multiple links. It is needed for example, when a site needs to change its domain name or
two web sites might merge etc.
– The handler method in controller will redirect to a new URL.
• Configuration
– The servlet configuration can be done in xml with spring bean definitions or use java implementation to config path and add resources
handler, resolve views.
– During startup, the servlet container reads the configuration.
• Validation
– Upon the user clicks the viewed page, it triggers ValidateController, where validation method takes the JavaBean object and return the
binding result indicating error or no error.
•