java 等效于基于 Spring 注释的配置中的 mvc:default-servlet-handler?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5062586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of mvc:default-servlet-handler in Spring annotation-based configuration?
提问by haxney
Is it possible to have the equivalent of <mvc:default-servlet-handler/>
defined in an AnnotationConfig(Web)ApplicationContext
? Right now I have:
是否可以<mvc:default-servlet-handler/>
在 an中定义等价物AnnotationConfig(Web)ApplicationContext
?现在我有:
@Configuration
@ImportResource("classpath:/mvc-resources.xml")
class AppConfig {
// Other configuration...
}
with just the following in my resources/mvc-resources.xml
:
在我的只有以下内容resources/mvc-resources.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:default-servlet-handler/>
</beans>
And it works as expected. Is it possible to do this without importing an XML file? It would be a nice way to cut down on some boilerplate.
它按预期工作。是否可以在不导入 XML 文件的情况下执行此操作?这将是减少一些样板文件的好方法。
采纳答案by haxney
After digging a bit deeper, I found out that this is a known problem and is addressed by annotation features in the upcoming Spring 3.1.
深入挖掘后,我发现这是一个已知问题,并在即将发布的Spring 3.1 中通过注释特性解决。
I solved my problem with the following code:
我用以下代码解决了我的问题:
@Configuration
@Import(FeatureConfig.class)
class AppConfig {
...
}
@FeatureConfiguration
class FeatureConfig {
@Feature
public MvcDefaultServletHandler defaultHandler() {
return new MvcDefaultServletHandler();
}
}
This does require using the milestone version of spring, though, but it seems to be the cleanest and preferred way of handling this.
不过,这确实需要使用 spring 的里程碑版本,但它似乎是处理此问题的最干净和首选的方式。
回答by Gumbit
If you are using Spring 3.1 with WebMvc, you can configure default servlet handling like this:
如果您将 Spring 3.1 与 WebMvc 一起使用,您可以像这样配置默认的 servlet 处理:
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
回答by Sean Patrick Floyd
I don't think you can do it out of the box, but you can probably copy what DefaultServletHandlerBeanDefinitionParser
does: Create a Bean of type DefaultServletHttpRequestHandler
and map it to the URL scheme /**
.
我不认为你可以做到开箱即用,但你也许可以复制什么DefaultServletHandlerBeanDefinitionParser
不:创建类型的一个Bean DefaultServletHttpRequestHandler
,并将其映射到URL方案/**
。
I'd say your Bean should subclass DefaultServletHttpRequestHandler
and do the mapping in a @PostConstruct
method.
我会说你的 Bean 应该子类化DefaultServletHttpRequestHandler
并在@PostConstruct
方法中进行映射。
回答by sourcedelica
@Bean
public DefaultServletHttpRequestHandler defaultServletHttpRequestHandler() {
return new DefaultServletHttpRequestHandler();
}
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
Map<String, String> urlMap = new ManagedMap<String, String>();
urlMap.put("/**", defaultServletHandlerName);
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
hm.setUrlMap(urlMap);
return hm;
}