In Spring's Web MVC framework : a DispatcherServlet that dispatches requests to handlers.The default handler is a very simple Controller interface, just offering a ModelAndView handleRequest(request,response) method.
<web-app>
...
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
|
With the above servlet configuration , you will need to have a file called '/WEB-INF/
test-servlet.xml' in your application; this file will contain all of your Spring Web MVC-specific components (beans).
The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications
The Spring DispatcherServlet has a couple of special beans it uses in order to be able to process requests and render the appropriate views. These beans are included in the Spring framework and can be configured in the WebApplicationContext
Controllers
controller is the org.springframework.web.servlet.mvc.Controller interface, the source code for which is listed below.
public interface Controller {
/**
* Process the request and return a ModelAndView object which the DispatcherServlet
* will render.
*/
ModelAndView handleRequest(
HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
|
As you can see, the Controller interface defines a single method that is responsible for handling a request and returning an appropriate model and view.
Command controllers provide a way to interact with data objects and dynamically bind parameters from the HttpServletRequest to the data object specified.
AbstractCommandController : This class does not offer form functionality;
AbstractFormController : an abstract controller offering form submission support.After a user has filled the form, the AbstractFormController binds the fields, validates the command object, and hands the object back to the controller to take the appropriate action.
SimpleFormController : a form controller that provides even more support when creating a form with a corresponding command object. The SimpleFormController let's you specify a command object, a viewname for the form, a viewname for page you want to show the user when form submission has succeeded.
AbstractWizardFormController : as the class name suggests, this is an abstract class - your wizard controller should extend it. This means you have to implement the validatePage(), processFinish() and processCancel() methods.
Handler mappings
Using a handler mapping you can map incoming web requests to appropriate handlers. the
SimpleUrlHandlerMapping or the
BeanNameUrlHandlerMapping you can use.
BeanNameUrlHandlerMapping:
A very simple, but very powerful handler mapping is the BeanNameUrlHandlerMapping, which maps incoming HTTP requests to names of beans, defined in the web application context.
<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
SimpleUrlHandlerMapping:powerful handler mapping - is the SimpleUrlHandlerMapping. This mapping is configurable in the application context
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/*/account.form=editAccountFormController
/*/editaccount.form=editAccountFormController
/ex/view*.html=helpController
/**/help.html=helpController
</value>
</property>
</bean>
Views
Resolving views - the ViewResolver interface: All controllers in the Spring Web MVC framework return a ModelAndView instance. Views in Spring are addressed by a view name and are resolved by a view resolver.
Example :
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
When returning "successaccount" as a viewname, this view resolver will hand the request over to the RequestDispatcher that will send the request to /WEB-INF/jsp/successaccount.jsp.