JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to map URLs to beans in Spring MVC?


This page shows how to map URL requests to spring bean controllers. These configurations are made with in xml based configuration file. In Spring MVC, org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping is the default handler mapping. You may or may not declare this bean, it is optional. Here is the sample xml based configuration for your reference:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	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-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">
 
	<context:component-scan base-package="com.java2novice.controllers" />
 
		<bean 
	class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
 
   <bean name="/employees" 
        class="com.java2novice.controllers.EmployeeController" />
 
   <bean name="/accounts" 
        class="com.java2novice.controllers.AccountsController" />
 
   <bean name="/query*" 
        class="com.java2novice.controllers.GenericQueryController" />
	
</beans>

We have mapped URIs with spring beans in the above xml based configuration file.

1) If you request /employees, then DispatcherServlet will forward the request to EmployeeController.
2) If you request /accounts, then DispatcherServlet will forward the request to AccountsController.
3) If you request /queryEmployee or /query{anything-here}, then DispatcherServlet will forward the request to GenericQueryController. The "*" allows any random text to be appear in the URL.

<< Previous Program | Next Program >>

Spring MVC Framework Examples

  1. Spring MVC hello world example
  2. Spring MVC REST hello world example
  3. How to map URLs to beans in Spring MVC?
  4. How to map URLs using annotations in Spring MVC?
  5. Spring MVC URI Template Patterns using Regular Expressions
Knowledge Centre
String Vs StringBuffer
We know that String is immutable object. We can not change the value of a String object once it is initiated. If we try to change the value of the existing String object then it creates new object rather than changing the value of the existing object. So incase, we are going to do more modificatios on String, then use StringBuffer. StringBuffer updates the existing objects value, rather creating new object.
Famous Quotations
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon.
-- Susan Erz

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.