BeanNameUrlHandlerMapping Example
BeanNameUrlHandlerMapping example in Spring 2.5 MVC framework
First of all, we will discuss about what HandlerMapping is. The HandlerMapping we can map incoming web request to the appropriate handler. When the user generate Request that reached the DispatcherServlet, the DispatcherServlet find the HandlerMapping object and map these Handler object to the Client request.
In this example we will discuss about BeanNameUrlHandlerMapping that provide mapping between request and the appropriate handler object on the basis of Url. We have used Http request as name of bean in the DispatcherServlet configuration file. Now we will provide an example that provided a way to you, how to use BeanNameUrlHandlerMapping in your application. Spring provides BeanNameUrlHandlerMapping by default if you have not include any HandlerMappings.
Step 1:
Now we will create a index.jsp inside the /WebContent/ directory that will have a hyperlink for generate Client request in the web browser. The code of the index.jsp is:
<%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <title>BeanNameUrlHandlerMapping</title> </head> <body> <center> <a href="beannameurlhandlermapping.html">BeanNameUrlHandlerMapping</a> </center> </body> </html> |
Step 2:
Now we will configure the web.xml for DispatcherServlet and set index.jsp as a welcome file. we will also add string.tld file with the help of <taglib> in the web.xml file. The code of the web.xml is:
<?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/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http: version="2.5"> <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> 0 <servlet-name>dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> 1 <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> 2 <taglib> <taglib-uri>/spring</taglib-uri> <taglib-location>/WEB-INF/spring.tld</taglib-location> 3 </taglib> </web-app> |
Step 3: 4
Now we will create a dispatcher-servlet.xml file inside the /WEB-INF/ project directory. The dispatcher-servlet.xml file contains maps incoming HTTP requests to names of beans by using BeanNameUrlHandlerMapping. The code that are use for this task is:
<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean name="/beannameurlhandlermapping.html" class="net.roseindia.web.BeanNameUrlHandlerMappingController"> 5 |
First bean setting provides BeanNameUrlHandlerMapping for the use as a HandlerMappings and second bean setting uses Http request as a name of the bean and provides class to handle this request. The BeanNameUrlHandlerMapping using Url of the Client is directly mapped to the Controller. The code of the dispatcher-servlet.xml is:
<?xml version="1.0" encoding="UTF-8"?> <beans 6 xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http: xmlns:p="http://www.springframework.org/schema/p"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 8 <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> 9 <property name="suffix"> <value>.jsp</value> </property> 0 </bean> <bean
id="defaultHandlerMapping"
class= <bean
name="/beannameurlhandlermapping.html"
class= </bean> </beans> |
Step 4: 2
Now we will create a controller class for handle the client request that's name BeanNameUrlHandlerMappingController.java. The BeanNameUrlHandlerMappingController.java class extends AbstractController that return ModelAndView object. The code of the BeanNameUrlHandlerMappingController.java is:
package net.roseindia.web;
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 BeanNameUrlHandlerMappingController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String Mess = "Bean Name Url Handler Mapping Example";
System.out.println(Mess);
ModelAndView modelAndView = new ModelAndView("success");
modelAndView.addObject("message", Mess);
return modelAndView;
}
}
Step 5:
The BeanNameUrlHandlerMappingController.java class return success object with message and the viewResolver resolve that append the suffix and prefix this object and return success.jsp so we will create success.jsp file inside the /WEB-INF/jsp/ folder that display message as response. The code of the success.jsp is: 3
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE
html
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 5 <title>BeanNameUrlHandlerMapping Example</title> </head> <body> 6 <center> <table> <tr> 7 <td> <b>${message}.</b> </td> 8 </tr> </table> </center> 9 </body> </html> |
Step 6: 0
Now we will run this example and see the output like:
After click on this link then a request will generate and display the response like: 1
Download code