SimpleUrlHandlerMapping maps controllers to URLs using a property collection defined in the Spring application context. You can declare SimpleUrlHandlerMapping in following 3 ways:
prop key
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/helloworld.html">helloWorldController</prop> </props> </property> </bean>
Set the instance of java.util.Properties to the property “mappings“, in which the key will be the url and value will be the id of the controller bean.
entry key
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/helloworld*" value-ref="helloWorldController"></entry> </map> </property> </bean>
Provide a map instance to SimpleUrlHandlerMapping by setting the property “urlMap“, in which the key of the entry will be url and value will be the controller class. We can also provide value-ref to indicate controller bean.
value
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /helloworld.html=helloWorldController /*/helloworld.html=helloWorldController /helloworld.htm=helloWorldController /helloworld=helloWorldController </value> </property> </bean>
Set the instance of java.util.Properties to the property “mappings” and provide handler IDs or names to URL patterns in value-ref.
Spring MVC is mapping the requested URL by the following conventions :
HelloWorldController -> /helloworld.html
HelloWorldController -> /myModule/helloworld.html
HelloWorldController -> /helloworld
Following example demonstrates Spring MVC SimpleUrlHandlerMapping.
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 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /helloworld.html=helloWorldController /*/helloworld.html=helloWorldController /helloworld.htm=helloWorldController /helloworld=helloWorldController </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 73.1
Image may be NSFW.
Clik here to view. Figure 73.1
The folder structure of the example is shown below in Figure 73.2
Image may be NSFW.
Clik here to view. Figure 73.2