Pages

Wednesday, November 17, 2010

Spring - Annotation Based Spring MVC

Assuming that the student knows basic web application structure and all the configuration files required for Basic web application.
Assuming that the student knows Servlets.

To create Annotation based Spring MVC web application please follow the below steps:

Step 1 : Update your web.xml file with following configuration:

    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

DispatcherServlet - Spring's Web MVC framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for upload files.
A central servlet that dispatches requests to controllers and offers other functionality facilitating the development of web applications.
Is an expression of the “Front Controller” design pattern.

It is completely integrated with the Spring IoC container and as such allows you to use every other feature that Spring has.


The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and create the beans defined there.

Step 2 : Create spring-mvc-servlet.xml under WEB-INF directory. (Observe that the file name should start with servlet-name of the DispatcherServlet-servlet.xml.)

Step 3 : The Basic spring-mvc-servlet.xml looks like below:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

</beans>

Step 4 : spring-mvc-servlet.xml file will contain all of your Spring Web MVC-specific components (beans). The exact location of this configuration file can be changed via a DispatcherServlet initialization parameter. You can specify that in web.xml like below:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring/spring-context.xml</param-value>
    </context-param>

Step 5 : Just as with any other view technology you're integrating with Spring, for JSPs you'll need a view resolver that will resolve your views. The most commonly used view resolvers when developing with JSPs are the InternalResourceViewResolver and the ResourceBundleViewResolver.

Add the below configuration in your spring-mvc-servlet.xml file: (I am using InternalResourceViewResolver here in this example)

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

Step 6 :  Here as we are going to use annotations instead of configuring our Controllers in xml files, we also need to add the following configuration in spring-mvc-servlet.xml file, so that container scans all the controller components at the initialization of DispatcherServlet and they will be available.

    <context:component-scan base-package="com.sritech.samples">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

Step 7 : Your web application lib directory also need the following spring jars:

spring.jar
spring-webmvc.jar
commons-logging.jar


These jars will be deployed to the server and they are also used during the build process.

The above configuration allows your DispatcherServlet to scan for all the controllers in the specified package (Ex: com.sritech.samples).

All above 6 Steps are one time configuration you have to do to create your annotation based Spring MVC application.

Example 1:- Create Welcome Page using SimpleWelcomeController and Simple JSP.

Step 1 : Create the controller class:

package com.sritech.samples.mvc.example1;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SimpleWelcomeController {

    @RequestMapping("/example1/welcome.htm")
    public void elementaryGreeter() {
    }
}

The @Controller annotation indicates that a particular class serves the role of a controller. There is no need to extend any controller base class or reference the Servlet API. You are of course still able to reference Servlet-specific features if you need to. The basic purpose of the @Controller annotation is to act as a stereotype for the annotated class, indicating its role. The dispatcher will scan such annotated classes for mapped methods, detecting @RequestMapping annotations.

The @RequestMapping annotation is used to map URLs like '/example1/welcome.htm' onto an entire class or a particular handler method.

Step 2 : Create the JSP.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Welcome to Spring MVC</title>
</head>

<body>
        Hello everyone! Welcome to Spring MVC: Example 1
</body>
</html>

Step 3 : Now create the war file and deploy into your Tomcat Server. (Any Web Server or application Server).

Step 4 : After the application is deployed go the below link:

http://server-name:portNumber/spring-mvc-samples/example1/welcome.htm

Step 5 : You will be seeing the following output on your screen:

Welcome to Spring MVCHello everyone! Welcome to Spring MVC: Example 1


Surprised!

Continue...

No comments:

Post a Comment