【问题标题】:No mapping found for HTTP request with URI [/myproject/] in DispatcherServlet with name 'appServlet' [duplicate]在名称为“appServlet”的 DispatcherServlet 中找不到具有 URI [/myproject/] 的 HTTP 请求的映射 [重复]
【发布时间】:2012-02-09 13:08:20
【问题描述】:

我是 Java 和 Spring 的绝对新手,我想从示例中学习。

我正在使用开箱即用的配置/安装

  • Mac OSX
  • 作为 IDE 的 Springsource 工具套件
  • Spring 2.8.1.RELEASE
  • vfabric-tc-server-developer-2.6.1.RELEASE

我尝试基于“Spring Template Project”生成一个新项目。然后我选择了“Spring MVC 项目”。示例项目已生成。之后,在不修改任何内容的情况下,我尝试通过“运行方式”执行 de“home.jsp”页面。 Web 服务器启动,最后我在控制台选项卡中收到错误。

未找到包含 URI [/myproject/] 的 HTTP 请求的映射 名称为“appServlet”的 DispatcherServlet

以及这些网页中的其他输出:

  • http://localhost:8080/myproject/WEB-INF/views/home.jsp
  • http://localhost:8080/myproject

在这里您可以看到我的项目结构的图片(为 STS 自动生成):

怎么了?

这里可以看到web.xml文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

root-context.xml 文件有此内容。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">

    <!-- Root Context: defines shared resources visible to all other web components -->

</beans>

最后 servlet-context.xml 有这个内容。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.mycompany.myapp" />

</beans:beans>

有人有办法解决吗?

【问题讨论】:

    标签: java spring spring-mvc sts-springsourcetoolsuite tcserver


    【解决方案1】:

    WEB-INF 下的所有东西都无法从外部访问,MVC 框架的重点是在调度到视图之前调用控制器,因此无论如何都不应该直接调用 JSP。

    而且您可能没有任何 Spring 控制器映射到根 URL,因此很明显,URL http://localhost:8080/myproject/ 上没有任何内容。

    【讨论】:

    • @AlfonsoDominguez @duffymo 自动生成的项目有一个名为“HomeController.java”的文件,其内容如下:` package com.mycompany.myapp; [...] OMITED PART [...] /** * 处理应用程序主页的请求。 / @Controller public class HomeController { /* * 只需通过返回其名称来选择要呈现的主视图。 */ @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Locale locale, Model model) { String str = "Server message"; model.addAttribute("serverTime", str);回家”; } } ` 你是这么说的吗?
    • 是的,这就是我的意思,您是否尝试访问 URL http://localhost:8080/myproject/home ?该 URL 似乎是您的配置文件中的正确 URL。
    【解决方案2】:

    Spring 的约定是假设DispatcherServlet 的web.xml 中的&lt;servlet-name&gt; 与Spring servlet 上下文XML 文件的开头匹配。将 servlet-context.xml 重命名为 appServlet-context.xml 看看是否有帮助。

    【讨论】:

    • 尝试我得到了同样的错误。实际上,在 web.xml 文件中,有一行指定了 servlet 的名称...现在我已经更改为您建议的那个(加上文件的重命名)...但结果是一样的. contextConfigLocation/WEB-INF/spring/appServlet/appServlet-context.xml
    【解决方案3】:

    向您的应用程序添加一个控制器,该控制器返回一个名为“home”的 ModelAndView 实例。然后为该控制器配置一些处理程序映射,并尝试使用类似于以下 URL 的 URL 访问您的 Web 应用程序:http://localhost:8080/myproject/home.do。更多信息可以在herehere找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多