Consolidating configuration using Spring MVC using ControllerClassNameHandlerMapping?

Following the directions of Spring Source and Spring's book in action, I am trying to configure Spring MVC to minimize xml configuration. However, according to Spring Source, you will configure ControllerClassNameHandlerMap

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<bean id="viewShoppingCart" class="x.y.z.ViewShoppingCartController">
    <!-- inject dependencies as required... -->
</bean>

Which seems completely useless to me, since it’s actually easier to use handlers to just install the beans manually, since this is about the same amount of XML.

Now the Spring book in action makes it sound like you need the first line of this block of code to use ControllerClassNameHandlerMapping, which would make it much more useful. However, I have not yet been able to get this to work.

Can anyone with Spring experience help me?

+3
source share
2 answers

There are actually two different things happening here:

  • mapping between urls and controllers
  • defining controllers as spring beans

For # 1, if you defined ControllerClassNameHandlerMapping as you did, it is about displaying a URL-to-controller. For example, http://example.com/context/home -> HomeController

For # 2, you can define a beans controller as you did. Or you can follow the path of using Spring 2.5 annotations for @Controllers and automatic posting, which eliminates the need for XML bean definitions. Or not, the choice is up to you.

, , ControllerClassNameHandlerMapping, URL- . .

, , , , - DefaultRequestToViewNameTranslator:

<!-- Generates view names based on the request url (e.g. "/home.htm" => "home", "/user/list.htm" => "user/list", etc.) -->
<bean id="viewNameTranslator" class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>

UrlBasedViewResolver:

<!-- Maps view names to tiles view definitions files.  E.g., "home" => "home", etc.  -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
+8

, ControllerClassNameHandlerMapping - , java .

0

Source: https://habr.com/ru/post/1705033/


All Articles