使用 ControllerClassNameHandlerMapping 使用 Spring MVC 进行约定优于配置?

发布于 2024-07-16 07:13:14 字数 733 浏览 10 评论 0原文

遵循 Spring 源 的说明和《Spring in Action》一书,我尝试以最小化 xml 配置的方式设置 Spring MVC。 然而,根据 Spring Source,这就是您设置 ControllerClassNameHandlerMap 的方式,

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

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

这让我觉得完全没用,因为使用处理程序手动设置 bean 实际上更简单,因为它的 XML 数量大约相同。

现在,《Spring in Action》一书听起来好像您只需要该代码块的第一行即可使用 ControllerClassNameHandlerMapping,这将使其更加有用。 但是,我还无法让它发挥作用。

有Spring经验的人可以帮我吗?

Following the directions from Spring Source and the book Spring in Action, I am trying to set up Spring MVC in a way that minimizes xml configuration. However according to Spring Source this is how you set up the ControllerClassNameHandlerMap

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

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

Which strikes me as being completely useless, as it is actually simpler to use the handlers to just set the beans manually, as it is about the same amount of XML.

Now the book Spring in Action makes it sound like all you need is the first line from that code block to use the ControllerClassNameHandlerMapping, which would make it far more useful. However, I have not yet been able to get this to work.

Can anyone with Spring experience help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昵称有卵用 2024-07-23 07:13:14

这里实际上发生了两件不同的事情:

  1. URL 和控制器之间的映射 将
  2. 控制器定义为 Spring bean

对于 #1,如果您像之前那样定义了 ControllerClassNameHandlerMapping,那么它将负责 URL 到控制器的映射。 例如, http://example.com/context/home -> HomeController

对于#2,您可以像您所做的那样定义控制器 bean。 或者,您可以继续使用 Spring 2.5 风格的 @Controller 注释和自动装配,这样就不再需要 XML bean 定义。 或者不,选择取决于你。

使用 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>

There are actually two different things going on here:

  1. the mapping between URLs and controllers
  2. the definition of controllers as Spring beans

For #1, if you define the ControllerClassNameHandlerMapping as you've done, that takes care of the URL-to-controller mapping. E.g., http://example.com/context/home -> HomeController

For #2, you can define the controller beans as you've done. Or you can go down the path of using the Spring 2.5-style annotations for @Controllers and auto-wiring, which eliminates the need for XML bean definitions. Or not, the choice is up to you.

What you avoid by using ControllerClassNameHandlerMapping is having to explictly map all your potential URLs to Controllers. We have used this successfully.

One other thing you might want to use is the 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"/>

We also use the 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>
吐个泡泡 2024-07-23 07:13:14

我不认为使用 ControllerClassNameHandlerMapping 是一个好的工程实践,因为它确实阻止了您对 Controller java 类进行重构工作。

I don't think using ControllerClassNameHandlerMapping is a good engineering practice, as it is really preventing you from doing refactoring work on the Controller java class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文