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

Spring MVC BeanNameUrlHandlerMapping

$
0
0

BeanNameUrlHandlerMapping maps from URLs to beans with names that start with a slash (“/”). This is the default implementation used by the DispatcherServlet. For Example :

<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<bean name="/helloworld.html" class="com.kruders.controller.HelloWorldController" />

In above code, if URI pattern /helloworld.html is requested, DispatcherServlet will forward the request to the “HelloWorldController“.

Following example demonstrates Spring MVC BeanNameUrlHandlerMapping.

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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController{
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		ModelAndView model = new ModelAndView("helloworld");
		model.addObject("message", "Hello World!!!");
		return model;
	}
}

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.

<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<bean name="/helloworld.html" class="com.kruders.controller.HelloWorldController" />
 
<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>

BeanNameUrlHandlerMapping is the default implementation used by the DispatcherServlet. If Spring can’t found any handler mapping, the DispatcherServlet will creates a BeanNameUrlHandlerMapping automatically.

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 69.1

Figure 69.1 Figure 69.1

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

Figure 69.2 Figure 69.2

You can download the source code of this example here.



Viewing all articles
Browse latest Browse all 10

Trending Articles