Quantcast
Channel: Kruders.com » Spring MVC
Viewing all articles
Browse latest Browse all 10

Spring MVC DefaultAnnotationHandlerMapping

$
0
0

DefaultAnnotationHandlerMapping maps request to class and/or methods that are annotated with @RequestMapping. For example :

@Controller
@RequestMapping("/helloworld.html")
public class HelloWorldController{
	@RequestMapping(method = RequestMethod.GET)
        ...
} 

Following example demonstrates Spring MVC DefaultAnnotationHandlerMappingMaps.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<properties>
    <spring.version>2.5.6</spring.version>
</properties>
 
<dependencies>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
 
</dependencies>

1. Controller

Now create HelloWorldController in com.kruders.controller package and write the following code.

HelloWorldController.java

package com.kruders.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("/helloworld.html")
public class HelloWorldController{
	
	@RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
  
        model.addAttribute("message", "Hello World!!!");
        return "helloworld";
  
    }
}

2. Create View

Create jsp folder in WEB-INF and create helloworld.jsp file in jsp folder

Now write the following code in helloworld.jsp file

${message}

3. Configuration

Create Spring Bean Configuration in WEB-INF folder and name it dispatcher-servlet.xml and add the following code.

<context:component-scan base-package="com.kruders.controller" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

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

4. Integrate Spring in Web App

To integrate Spring in Web Application, write the following code in web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
 
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
 
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

5. Run

Now when you run the project, following screen will be displayed as in Figure 72.1

Figure 72.1 Figure 72.1

The folder structure of the example is shown below in Figure 72.2

Figure 72.2 Figure 72.2

You can download the source code of this example here.



Viewing all articles
Browse latest Browse all 10

Trending Articles