Spring MVC SimpleUrlHandlerMapping tutorial with example


What is SimpleUrlHandlerMapping

Mappings to bean names can be set via the "mappings" property, in a form accepted by the java.util.Properties class, like as follows:

/welcome.html=ticketController 
/show.html=ticketController 

The syntax is PATH=HANDLER_BEAN_NAME. If the path doesn't begin with a slash, one is prepended.

Example
<?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-3.0.xsd 
           http://www.springframework.org/schema/util 
           http://www.springframework.org/schema/util/spring-util-3.0.xsd  
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <!-- ************************************************************* -->
 
      
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  
    <property name="mappings">
	
      <props>
	  
        <prop key="/simpleUrlOne.htm">firstController</prop>
        <prop key="/simpleUrlTwo.htm">secondController</prop>
		
      </props>
	  
    </property>
	
  </bean>
   
	<!-- ************************************************************* --> 
	
	<bean id="firstController" class="com.candidjava.springmvc.FirstController" />
  	<bean id="secondController" class="com.candidjava.springmvc.SecondController" /> 
  	
  	<!-- ************************************************************* --> 
	 
	 
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
      <value>/WEB-INF/pages/</value>
    </property>
	
    <property name="suffix">
      <value>.jsp</value>
    </property>
	
  </bean>
	
	<!-- ************************************************************* -->
</beans>

Screenshot
            
            
            


Download
            SimpleUrlHandlerMapping Maven zip
            SimpleUrlHandlerMapping war



Comments


©candidjava.com