使用 ControllerClassNameHandlerMapping 使用 Spring MVC 进行约定优于配置?
遵循 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里实际上发生了两件不同的事情:
对于 #1,如果您像之前那样定义了 ControllerClassNameHandlerMapping,那么它将负责 URL 到控制器的映射。 例如, http://example.com/context/home -> HomeController
对于#2,您可以像您所做的那样定义控制器 bean。 或者,您可以继续使用 Spring 2.5 风格的 @Controller 注释和自动装配,这样就不再需要 XML bean 定义。 或者不,选择取决于你。
使用 ControllerClassNameHandlerMapping 可以避免将所有潜在的 URL 显式映射到控制器。 我们已经成功地使用了这一点。
您可能想要使用的另一件事是 DefaultRequestToViewNameTranslator:
我们还使用 UrlBasedViewResolver:
There are actually two different things going on here:
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:
We also use the UrlBasedViewResolver:
我不认为使用 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.