Search This Blog

Friday, 9 November 2012

Spring MVC without URL Mappings ?

In an earlier example we saw that Spring uses a single front-controller servlet. The Servlet then delegates the request to the controllers. This was achieved by mapping the controllers to urls.
<bean id="urlMapping"
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <value>
            /**/anything.do=defaultController
        </value>
    </property>
</bean>
Above we have mapped our bean ids to different URL expressions. It is not necessary to define a separate mapping bean at all times. We could have defined the above as follows:
<bean name="/**/anything.do" class="com.mvc.controller.DataWelcomeController"/>
The single line is sufficient to do the work of the above code. We have defined an instance of the controller bean and 'identified' it with the URL. Due to the special characters like slash and asterix the id would end up with an invalid value. Hence the name attribute is used.
In the above code Spring internally uses the BeanNameUrlHandlerMapping class to identify all such mappings. The code documentation for the class gives a very precise explanation of the various Mapping handlers available:
Implementation of the org.springframework.web.servlet.HandlerMapping
  interface that map from URLs to beans with names that start with a slash ("/"),
  similar to how Struts maps URLs to action names.
 
  This is the default implementation used by the
  {@link org.springframework.web.servlet.DispatcherServlet}, along with
  {@link org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping}
  (on Java 5 and higher). Alternatively, SimpleUrlHandlerMapping allows for
  customizing a handler mapping declaratively.
 
  The mapping is from URL to bean name. Thus an incoming URL "/foo" would map
  to a handler named "/foo", or to "/foo /foo2" in case of multiple mappings to
  a single handler. Note: In XML definitions, you'll need to use an alias
  name="/foo" in the bean definition, as the XML id may not contain slashes.
Other URL Handler mappings include:
  1. ControllerClassNameHandlerMapping
  2. CommonsPathMapHandlerMapping
in the next post we shall attempt to use multiple handler mappings together.

4 comments:

  1. When things get rough just stay calm and ask assistance from God and he'll give you peace. Visit my site for more information.

    triciajoy.com

    www.triciajoy.com

    ReplyDelete

  2. I would like to share it with all my friends and hope they will like it too.

    Gail
    www.imarksweb.org

    ReplyDelete
  3. Life is full of many challenges. Challenges that will make you or break you depending on how you handle it.
    Visit my site for more updates. God Bless to your site.

    n8fan.net

    www.n8fan.net

    ReplyDelete
  4. If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you need to watch this video
    right away...

    (VIDEO) Win your ex back with TEXT messages?

    ReplyDelete