Spring MVC 3 example XML config
- 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 example XML config
- 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)
In this tutorial we are targeting AbstractController for xml based configuration. Spring MVC also supports annotation based configuration as of Spring 3. All of spring's controllers are inherited from AbstractController, The AbstractController provides the basic building blocks. You can extend the AbstractController class and the build your own controller from scratch. It is used for simple stuff like returning a resource to the client or redirecting.
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
In our ApplicationContext of Spring we register our controller with the name '/hello.htm'. By default spring 3 mvc will use BeanNameUrlHandlerMapping to resolve the url to controller class mapping. So our controller will listen to the url '/hello.htm'. The view resolver will resolve jsp pages from view name
web.xml
Now we have to register the dispatcherServlet of spring to work as a frotController.
Jsp page
Screenshots






Latest Posts
Latest Comments
Tag cloud