【问题标题】:RequestMapping in spring mvcSpring mvc中的RequestMapping
【发布时间】:2013-03-01 01:06:23
【问题描述】:

我正在尝试理解 spring mvc 中的 requestmapping,我有一个应用程序,实际上我才刚刚开始使用 spring mvc。

我有这两个网址

最后一个带有正斜杠的第一个似乎工作正常。 http://localhost:8080/contactmanager/index/

末尾没有正斜杠的第二个不起作用

http://localhost:8080/contactmanager/index

第二个给我“HTTP 状态 404 -”错误,我如何强制应用程序在 url 末尾附加正斜杠?

控制器中的方法是这样的

@RequestMapping("/index")
public String listContacts(Map<String, Object> map) {

    map.put("contact", new Contact());
    map.put("contactList", contactService.listContact());
    //org.springframework.web.context.ContextLoaderListener
    //org.springframework.web.context.ContextLoaderListener
    //org.springframework.web.servlet.DispatcherServlet
    //org.springframework.web.servlet.DispatcherServlet
    return "contact";
}

我的 web.xml 看起来像这样

<servlet>
    <servlet-name>contactmanager</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/contactmanager-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>contactmanager</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

提前致谢。

【问题讨论】:

  • @user1428716 问题是,如果我在结尾处使用正斜杠 (/) 编写 URL,例如 index/ ,我会得到它的工作,但是当我像 index 一样省略末尾的斜杠时,那么我得到 404 ,这与配置 web.xml 的方式有关吗?

标签: spring model-view-controller


【解决方案1】:

使用java配置:

@Configuration
@EnableWebMvc
public class WebConfig {}

或使用 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.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven />

来自Spring's reference documentation

上面注册了一个RequestMappingHandlerMapping,一个 RequestMappingHandlerAdapter 和一个 ExceptionHandlerExceptionResolver (除其他外)支持处理带有注释的请求 使用 @RequestMapping 等注解的控制器方法, @ExceptionHandler 等。

RequestMappingHandlerMapping 中有一个有用的布尔标志 - 来自 Javadoc 的说明:

/**
 * Whether to match to URLs irrespective of the presence of a trailing slash.
 * If enabled a method mapped to "/users" also matches to "/users/".
 * <p>The default value is {@code true}.
*/
public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch) {
   this.useTrailingSlashMatch = useTrailingSlashMatch;
}

由于默认情况下已启用此功能,因此请确保您的配置使用 RequestMappingHandlerMapping

如果这对您没有帮助,请检查 spring 的默认 servlet 是否已配置:&lt;mvc:default-servlet-handler/&gt; 并尝试添加到 web.xml

<!-- Disables Servlet Container welcome file handling. Needed for compatibility with   Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
   <welcome-file></welcome-file>
</welcome-file-list>

【讨论】:

  • 谢谢,我想我在spring mvc中对整个请求映射没有清晰的映射,现在我明白了,你可以指定控制器类或方法上的映射,方法中嵌套意味着你追加类上的路径到方法中的路径。感谢领导
  • 对于那些使用扩展WebMvcConfigurerAdapter的配置类,重写方法configurePathMatch(PathMatchConfigurer configurer)并设置configurer.setUseTrailingSlashMatch(false);。默认值为true,如果您正在阅读此问题和答案,您毫无疑问会发现。
猜你喜欢
  • 1970-01-01
  • 2015-06-22
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多