Spring MVC
with Annotations
·
The configurations in xml can be decrease by
using annotations
·
The Basic annotations for Spring mvc is
1.
@controller
·
This is used to makes the class as Controller
class
·
We can remove the Controller configuration from
Xml
<!-- 1.
Controller Class Conguration -->
<bean name="/hello"
class="controller. HelloController
"></bean>
·
To activate annotations we must place following
code in Spring-servlet.xml
<!-- 2. Controolers
Base Pkg -->
<context:component-scan base-package="controller"></context:component-scan>
2.
@RequestMapping
·
It is used for configure the action
·
It is replacement of HandlerMapping class
<!-- 1. Handler
Class Conguration -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
For MVC related annotations we must
place following code in spring-servlet.xml
<!-- 1. Handler
Class Conguration -->
<mvc:annotation-driven></mvc:annotation-driven>
Most commonly used
annotations
·
@Controller
·
@RequestMapping
·
@PathVariable
·
@RequestParam
·
@ModelAttribute
·
@InitBinder
Here the Sample Example Using Annotations
Web.xml
<?xml version="1.0"
encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>AnnoEx</display-name>
<servlet>
<servlet-name>spring</servlet-name>----
(3)
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> ---- (2)
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Spring-Servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 1.handlerMapping Repalcement
-->
<mvc:annotation-driven></mvc:annotation-driven> ---- (4)
<!-- 2. Controolers Base Pkg
-->
<context:component-scan base-package="controller"></context:component-scan> ---- (5)
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> ---- (10)
<property name="prefix"
value="/WEB-INF/"></property>
<property name="suffix"
value=".jsp"></property>
</bean>
</beans>
HelloController.java
package controller;
import
org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.servlet.ModelAndView;
@Controller ---- (6)
public class HelloController {
@RequestMapping("/welcome") ---- (7)
public ModelAndView helloMethod(){
System.out.println("*************
Start ************************");
ModelAndView model = new ModelAndView("Hello"); ---- (8)
model.addObject("msg", "Hello
Annotation");
System.out.println("*************
END ************************");
return model; ---- (9)
}
}
Hello.jsp----
(11)
<h1>${msg}</h1>
Output : http://localhost:8080/AnnoEx/welcome
Hello Annotation ---- ---- (12)
2.
DisptacherServlet is loaded by the container, with
the name spring.
3.
DisptacherServlet searches for spring-servlet.xml
4.
spring-servlet.xml by reading <mvc:annotation-driven></mvc:annotation-driven>
this is using annotation configuration
5.
<context:component-scan base-package="controller">
means all the controller classes contains under this package
.HandlerMapping searches controllers in this package
6.
@Controller , by reading this container will
understand it’s a Controller, and searches for /welcome action in this class
7.
@RequestMapping("/welcome") is
compare with requested URL , if match ..below method will execute
8.
Setting result page as “Hello” as name of the
page
9.
Returns model object.
10.
ViewResolver will searches for “Hello.jsp”,
based on name and suffix
11.
Hello.jsp will execute
12.
Output displayed