【问题标题】:Spring MVC URL not getting resolved correctlySpring MVC URL 未正确解析
【发布时间】:2013-10-07 03:29:57
【问题描述】:

我在 Eclipse 中创建了一个 Spring 项目。问题是,该 URL 没有以我认为应该的方式得到解决。

web.xml

<servlet>
          <servlet-name>mvc-dispatcher</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
     <servlet-name>mvc-dispatcher</servlet-name>
     <url-pattern>/files/*</url-pattern>
     <url-pattern>/filestest/*</url-pattern>
</servlet-mapping>

这是我的 mvc-dispatcher-servlet.xml

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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      
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     <mvc:annotation-driven />
     <context:component-scan base-package="com.github.myproject.controllers" />

</beans>

这是我的控制器文件:

@Controller
@RequestMapping("/filestest")
public class TestController {
    @RequestMapping(value="", method=RequestMethod.GET)
    public @ResponseBody String getBase() {
        System.out.println("Base");
        return "Base";
    }

    @RequestMapping(value="/", method=RequestMethod.GET)
    public @ResponseBody String getRoot() {
        System.out.println("Root");
        return "Root";
    }

    @RequestMapping(value="abc", method=RequestMethod.GET)
    public @ResponseBody String getABC() {
        System.out.println("ABC");
        return "ABC";
    }

    @RequestMapping(value = "/abc", method=RequestMethod.GET)
    public @ResponseBody String getBaseAbc() {
        System.out.println("Base ABC");
        return "Base ABC";
    }

    @RequestMapping(value="/{pathVar}", method=RequestMethod.GET)
    public @ResponseBody String getPathVar(@PathVariable(value="pathVar") String pathVar) {
        System.out.println(pathVar);
        return pathVar;
    }
}

这是我得到的输出

http://mylocalhost.com/filestest/abc - 404

http://mylocalhost.com/filestest/ - 404

http://mylocalhost.com/filestest - 有效输出 - “基础”

根据我对Spring documentation 的理解,web.xml 应该将所有 /filestest 请求路由到 DispatcherServlet -> 将请求路由到我的 Controller -> 然后应该匹配正确的方法。

谁能帮我弄清楚为什么我在尝试部署和测试我的应用程序时收到 404 File not found URLs 错误 - http://mylocalhost.com/filestest/abchttp://mylocalhost.com/filestest/

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    从控制器的RequestMapping 注释中删除/filestest

    Spring 仅使用映射路径部分 (*),因此您的 RequestMapping 中无需使用 servlet 前缀。

    【讨论】:

    • 是的。这就是原因。因此,看起来,给定一个 URL,Spring 将其拆分为与 web.xml 中的“url-mapping”匹配的部分,与 Controller 的 RequestMapping 和方法的 RequestMapping 匹配的部分。
    猜你喜欢
    • 2018-05-22
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多