8

我正在使用<mvc:annotation-driven/>并且我想配置RequestMappingHandlerMapping为禁用useTrailingSlashMatch. 当我声明另一个 RequestMappingHandlerMapping 时,我将结束 2 RequestMappingHandlerMapping。我该如何配置RequestMappingHandlerMapping

4

4 回答 4

9

正如您已经注意到的,这在 xml 中是可行的,方法是删除mvc:annotation-driven和替换整个 xml 等效项:

<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>
            <property name="validator">
                <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
            </property>
        </bean>
    </property>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
            <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>
        </list>
    </property>
</bean>

<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useTrailingSlashMatch" value="true"></property>
</bean>
于 2012-11-29T15:12:33.083 回答
1

您可以尝试使用 Java 配置来覆盖RequestMappingHandlerMapping值吗

@Configuration
@ComponentScan(basePackages = "base.package.name")
public class WebAppConfig extends WebMvcConfigurationSupport {

    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping rmh = super.requestMappingHandlerMapping();
        rmh.setUseTrailingSlashMatch(false);
        return rmh;
    }
}
于 2012-11-29T13:45:43.343 回答
1

如果您想要一个不涉及在 Spring 中复制功能的解决方案,那么您可以覆盖 DisplatcherServlet。在 Servlet 3.0 容器中,这可能看起来像:

@WebServlet(name="spring-dispatcher", loadOnStartup=1, urlPatterns={"/"},
        initParams={
            @WebInitParam(name="contextConfigLocation", 
            value="/WEB-INF/spring/spring-dispatcher-servlet.xml")})
public class MyDispatcherServlet extends DispatcherServlet {

    @Override
    protected void initStrategies(ApplicationContext context) {
        super.initStrategies(context);
        for (RequestMappingInfoHandlerMapping handlerMapping 
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(
                    context, RequestMappingInfoHandlerMapping.class, true, false).values()) {

            handlerMapping.setUseTrailingSlashMatch(false);
        }
    }
}
于 2013-03-29T10:46:21.593 回答
0

将以下内容添加到您的 spring 配置文件以切换该useTrailingSlashMatch字段。

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useTrailingSlashMatch" value="true">
    </property>
</bean>
于 2012-11-27T10:03:16.677 回答