Spring MVC 3 example annotation 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 AOP @AfterThrowing annotation example
- Spring AOP @After annotation example
- Spring AOP annotation configuration example
- Spring AOP @Before annotation example
- Spring AOP @After returning annotation example
- Spring AOP @Around annotation example
- Spring auto component scanning
- Spring MVC 3 example annotation config
- Configure hibernate with Spring XML
- 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 java tutorial, we will address Spring MVC's annotation based configuration. We can easily create our controller with a simple class level annotation. Then we'll have to create a method that will handle our request/response and forward that request/response to a particular view. With spring's view resolver. In this example, we will add an Object (a message String) to the request and display it.
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.
@Controller
In spring MVC any java POJO can be a controller. The only thing we have to do is annotate our class with @Controller, and register component scanning in our application context and spring will automatically make it a MVC controller. Next we annotate our class with @RequestMapping("hello") witch means that every method in our class automatically maps to /hello. If we annotate another @RequestMapping("hello") at method level then the controller can be accessed at /hello/hello. you get the picture.. In the request mapping annotation we can also specify a RequestMethod like: GET, POST, DELETE, PUT.
We return a ModelAndView instance witch we can add Objects to and specify a view name for our view resolver to handle.
package org.camelcode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello, World!");
return "index";
}
}
Screenshots






Latest Posts
Latest Comments
Tag cloud