10:11 am - Wednesday April 16, 2014

Spring MVC with BeanNameUrlHandlerMapping Tutorial and Example

BACK


Spring MVC with BeanNameUrlHandlerMapping Tutorial  and Example

dispatcher-servlet.xml


<beans 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://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

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

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

	<bean name="/welcome.htm" class="com.candidjava.controller.HelloWorldController" />

	<bean name="/process*.htm" class="com.candidjava.controller.HelloWorldController" />

</beans>

HelloWorldController.java

package com.candidjava.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 request,
			HttpServletResponse response) throws Exception {

		ModelAndView model = new ModelAndView("HelloWorldPage");
		model.addObject("msg", "hello world");

		return model;
	}

}

HelloWorldPage.jsp


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h1>Spring MVC Hello World Example</h1>

<h2>${msg}</h2>

</body>
</html>

index.jsp


<div align="center">
<h3>
<u>Bean Name URLHandler Mapping Example</u>
</h3>
</div><br/>

<div align="center">

<a href="welcome.htm">Click</a> here to call welcome.htm <br/><br/>
<a href="process*.htm">Click</a> here to call process*.htm

</div>

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/j2ee" xmlns:javaee="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <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>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

Output screenshots

Download Sourcecode  + Spring_mvc_jar

BACK

Filed in: Z-A Spring Codes

Comments are closed.

Social Widgets powered by AB-WebLog.com.