到目前为止,<mvc:annotation-driven />
给我带来了很多麻烦,所以我想摆脱它。虽然spring framework docs clearly say what it is supposed to be doing,但标签列表实际上是summar <mvc:annotation-driven />
。
所以我不得不删除<mvc:annotation-driven />
,现在收到错误
WARN o.s.web.servlet.PageNotFound - 找不到HTTP请求的映射 URI [/ webapp / trainees] in 具有名称的DispatcherServlet 'workoutsensor'
对于所有应由控制器类解析的Url(在本例中为./trainees
)。有什么建议我可以阅读更多关于<mvc:annotation-driven />
的内容吗?我非常想知道<mvc:annotation-driven />
确切代表了哪些标签。
答案 0 :(得分:26)
您可以使用BeanPostProcessor
自定义<mvc:annotation-driven />
定义的每个bean。 javadoc现在详细说明了标签注册的所有bean。
如果您真的想摆脱它,可以查看org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
您可以看到它定义的是哪些bean。我做过这个'练习'(不是针对所有人,而是针对我需要的人),所以他们就是这样:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.yourpackage.web.util.CommonWebBindingInitializer" />
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.feed.AtomFeedHttpMessageConverter" />
<bean class="org.springframework.http.converter.feed.RssChannelHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
<!-- bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /-->
</list>
</property>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
现在,您可以看到CommonWebBindingInitializer
。您必须创建此类,才能使用转换和验证:
public class CommonWebBindingInitializer implements WebBindingInitializer {
@Autowired
private Validator validator;
@Autowired
private ConversionService conversionService;
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.setValidator(validator);
binder.setConversionService(conversionService);
}
}
到目前为止,这对我来说很好。随时报告任何问题。
答案 1 :(得分:5)
如果你想避开mvc:annotation-driven
标记,你可以自己创建DefaultAnnotationHandlerMapping
和AnnotationMethodHandlerAdapter
bean,但听起来好像找到麻烦的根源会更好标签本身。
您的问题有什么症状?你想用Spring MVC应用程序做什么?
如果您想知道使用mvc:annotation-driven时幕后发生了什么,请参阅AnnotationDrivenBeanDefinitionParser
.parse()
方法。
答案 2 :(得分:3)
老问题我知道,但这可能对某人有所帮助。感谢此页面上的帖子以及over here,我已使用以下内容替换Roo 1.2应用中的注释驱动标记。他们踢我的是在roo应用程序列表视图中需要支持类型转换。
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean id="conversionServiceExposingInterceptor"
class="org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor">
<constructor-arg ref="conversionService" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="order" value="0" />
<property name="interceptors">
<list>
<ref bean="conversionServiceExposingInterceptor" />
</list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
<property name="validator" ref="validator" />
</bean>
</property>
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
</list>
</property>
</bean>
答案 3 :(得分:2)
虽然压倒一切,但还要考虑自定义执行管理覆盖。否则,所有自定义异常映射都将失败。您必须将messageCoverters与list bean重用:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<util:list id="messageConverters">
<bean class="your.custom.message.converter.IfAny"></bean>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</util:list>
<bean name="exceptionHandlerExceptionResolver"
class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
<property name="order" value="0"/>
<property name="messageConverters" ref="messageConverters"/>
</bean>
<bean name="handlerAdapter"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
<property name="validator" ref="validator" />
</bean>
</property>
<property name="messageConverters" ref="messageConverters"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>