Professional Java Development with the Spring Framework
Common Layout of a Spring MVC Web Application
Spring MVC is an extremely flexible piece of technology. Most use cases don't require much customization or configuration, but that flexibility is likely to help you a great deal when you encounter tougher requirements. Up until now you've seen what is involved with a request being handled by Spring MVC. First, a HandlerMapping is inspected to return a handler (or controller). After the Controller has returned a ModelAndView, the ViewResolver provides a concrete view instance after which Spring lets the view do its job — render a response to the client. It's that simple.
The simplest configuration possible for a web application using Spring MVC is one where only controllers, views, and a simple view resolver are defined, all in the WebApplicationContext:
<bean /> <bean name="someView" > <property name="url"><value>/WEB-INF/jsp/someView.jsp</value></property> </bean> <bean />
This approach will not scale to larger applications. The following is a list of all types of components in Spring Web MVC along with their respective defaults and common bean names. You will learn about them shortly. For now it's important to realize Spring does not require you to know and use all of them. Most are intended for use only in more demanding applications requiring internationalization or the uploading of files. While reading the rest of this chapter, pick what you need and refer to this part of the chapter every now and then to see where the component you're reading about fits in.
Important | Remember that the most important parts of your web tier are the Controllers returning Models and the View returned to your client. |
Component Type | Defaults To | Remarks |
---|---|---|
Controller | None | Controllers are the components providing the actual behavior. You will have to implement them yourself. |
ViewResolver | InternalResourceViewResolver | Request will be dispatched using the RequestDispatcher, which causes the servlet container to pick it up again and redirect it to a servlet or JSP. |
HandlerMapping | BeanNameUrlHandlerMapping | Provides elegant in-context resolving of controllers. Using this, you cannot specify interceptors, however. |
LocaleResolver | AcceptHeaderLocaleResolver | Takes the locale from the client’s request header. Others are available based on cookies, session storage, and so on. |
MultiPartResolver | None | Needed only if you want to upload files. Implementations available for Commons and COS file upload packages. |
HandlerExceptionResolver | None | Needed to provide appro- priate responses to exceptions generated by Controllers. One imple- mentation available: SimpleMappingException Resolver. |
Note that the previous table lists only the most important concepts used in Spring Web MVC. Later in the chapter we will cover some of the more advanced functionality used only in rare situations.