2

達成したいかなり単純なタスクがありますが、Spring MVC ルーティングに関する情報が見つからないようです。ビューへのパスをルーティングする非常に単純なコントローラーがあります。

@Controller
@RequestMapping(value = "/help")
public class HelpController {

    private static final String HELP = "help";

    @RequestMapping(method = RequestMethod.GET)
    public String help(Model model, Locale locale) {
        model.addAttribute("locale", locale);
        return HELP;
    }
}

http://mysite.com/help.some.extension.is.enteredの場合は 404 をスローしたいのですが、Spring は例を /help に解決するようです。javadoc には、@RequestMapping アノテーションは単なるサーブレット URI マッピングであると書かれていますが、/help は完全に一致する必要があることを意味すると考えました。明確化をいただければ幸いです。

4

4 に答える 4

1

Spring 4 の場合、解決するのは非常に簡単です。

<mvc:annotation-driven>
    <mvc:path-matching suffix-pattern="false" />
</mvc:annotation-driven>

したがって、引き続き構成に使用できmvc:annotation-drivenます。

于 2014-05-09T20:09:39.017 に答える
0

Spring 3.0.X ではuseDefaultSuffixPatternプロパティを使用できます。

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="useDefaultSuffixPattern" value="false" />
</bean>

削除する必要があります</mvc:annotation-driven>

SPRING MVC の URL パターン制限を参照

于 2012-06-05T04:49:56.743 に答える
0

私が考えることができる最善の方法は、次のように、サフィックスパスを考慮しないように RequestMappingHandlerMapping を明示的に構成することです。

<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useSuffixPatternMatch" value="false"></property>
</bean>

ただし、Spring MVC を使用して構成した場合mvc:annotation-driven、これは機能しません。handlerAdapter 定義全体を展開する必要があります。これは、これらの行に沿って行うのはそれほど難しくありません (これは完全ではありませんorg.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser。定義全体):

<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>
            <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>
        </list>
    </property>
</bean>

<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useSuffixPatternMatch" value="false"></property>
</bean>
于 2012-06-04T20:33:05.170 に答える
0

@RequestMapping アノテーションで言及できます

サーブレット URL パターンのみと同じです。

@Controller
public class HelpController {

    private static final String HELP = "help";

    @RequestMapping(value = "/help" method = RequestMethod.GET)
    public String help(Model model, Locale locale) {
        model.addAttribute("locale", locale);
        return HELP;
    }

    @RequestMapping(value = "help/*" method = RequestMethod.GET)
    public String helpWithExtraWords() {
        return "error";
    }
}
于 2012-06-04T20:12:30.427 に答える