Spring MVC 3 SimpleUrlHandlerMapping
- Spring MVC 3 BeanNameUrlHandlerMapping
- Spring MVC 3 SimpleUrlHandlerMapping
- Spring MVC 3 example XML config
- Spring MVC 3 Form Validation XML config
- Spring MVC 3 example annotation config
- Spring MVC 3 Form processing Annotation config
- Spring MVC 3 Form processing XML config
- Spring MVC 3 SimpleUrlHandlerMapping
- Spring Constructor Injection
- Spring Setter Injection
- Passing parameters in an aspect with XML
- Spring auto component scanning
- Spring AOP @AfterThrowing annotation example
- Before Aspect XML configuration
- AfterThrowing Aspect XML configuration
- Spring AOP @After returning annotation example
- Spring AOP @Before annotation example
- Around Aspect XML configuration
- After Aspect XML configuration
- Spring AOP terminology
- Spring AOP @Around annotation example
- Spring AOP @After annotation example
- Spring AOP annotation configuration example
- Spring MVC 3 Form Validation XML config
- Spring MVC 3 BeanNameUrlHandlerMapping
- Spring MVC 3 Form processing Annotation config
- Spring MVC 3 Form processing XML config
- AfterReturning Aspect XML configuration
- AOP examples XML configuration
- Spring dependency injection multiple syntax
- Sending Mails With Spring MailSender
- Working with Spring's Application Context
- Wiring collections with spring
- Spring 3.0 java based configuration
- Spring Java Config @Import example
- Spring MVC 3 SimpleUrlHandlerMapping
- Spring MVC 3 example XML config
- Spring MVC 3 example annotation config
- Configure hibernate with Spring XML
- Configure hibernate in Spring Annotations
- Hibernate Named Query CRUD example
- Automatically create update database with Hibernate and Spring
Comments (0)
By default spring uses BeanNameUrlHandlerMapping, buth Spring MVC 3 also provides SimpleUrlHandlerMapping that also can be used to map urls to the controller. When a request comes in, the DispatcherServlet will hand it over to the handler mapping to let it inspect the request and come up with an appropriate HandlerExecutionChain. Then the DispatcherServlet will execute the handler and interceptors in the chain (if any). The concept of configurable handler mappings that can optionally contain interceptors (executed before or after the actual handler was executed, or both) is extremely powerful. A lot of supporting functionality can be built into custom HandlerMappings.
Maven configuration
You need the following jar files. So either add this pom.xml file to your application or download and add the jar files to your class-path.
AbstractController
Here, our HelloController class extends AbstractController class, that makes our class function as a controller and able to handle a request/response. If you extend AbstractController, you are omitted to override the handleRequestInternal method which will handle the request. We must return a ModelAndView object, this object contains a view name in our example 'index' which returns the index.jsp page. We also add an Object witch the identifier 'message'.
package org.camelcode.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("message", "Hello, World!");
return modelAndView;
}
}
Spring ApplicationContext
We register our controller with the <bean/>. Then we register a SimpleUrlHandlerMapping witch is automatically detected by the dispatcher servlet. We can either register our url's with <property name="urlMap"/> or with <property name="mappings"/>. So we can map the same controller to different url's. Next is the InternalResourceViewResolver witch will forward the right view from the controller.
helloController helloController
web.xml
JSP Page
Calling the following url's you can access the controller and display the correct view:
- /hello.htm ?> /hello.htm
- /sayHello.htm ?> /sayHello*
- /sayHelloToAll.htm ?> /sayHello*
- /welcome.htm ?> /welcome.htm
Screenshots






Latest Posts
Latest Comments
Tag cloud