Difference between BeanNameUrlHandlerMapping Simpl

2019-05-11 17:28发布

问题:

What is the difference between Spring BeanNameUrlHandlerMapping and SimpleUrlHandlerMapping?

After going through some site I understand that BeanNameUrlHandlerMapping is the default HandlerMapping for DispatcherServlet.

Also in BeanNameUrlHandlerMapping you can directly map url with Controller.

but same thing can be done with SimpleUrlHandlerMapping as well.

I know something but not that much clear, that it has to do something with

name accepts / but id can't.....but i am confused.

what is the exact diffrence? what is the need of SimpleUrlHandlerMapping?

please explain...Thanks.

回答1:

We have to register more than one urls if we want map more than one url to be mapped to a single controller class and this is not a good way to work with Spring IoC.

SimpleUrlHandlerMapping is a simple way to define url mapping using a map or property bean. This simplify the url mapping in Spring MVC.

Example BeanUrlHandlerMapping

<bean name="/hello.htm" class="com.raistudies.ui.comtroller.HelloController"/>

<bean name="/sayHello*" class="com.raistudies.ui.comtroller.HelloController"/>

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

Important:

  • Is necessary to define a bean for each url
  • Spring container has a bean by url. Although all url are served by the same bean

Example SimpleUrlHandlerMapping

<bean id="helloController" class="com.raistudies.ui.controller.HelloController"/>

  <bean id="urlHandler" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/hello.htm" value-ref="helloController"/>
                <entry key="/sayHello*" value-ref="helloController"/>
                <entry key="/welcome.html" value-ref="helloController"/>
                <entry key="/welcomeUser*" value-ref="helloController"/>
            </map>
        </property>
    </bean>

Important:

  • Using a single bean for URLs that use the same controller.
  • Is not necessary define a bean for each url