本文介绍了在DispatcherServlet中找不到带有URI [/ myproject /]的HTTP请求的映射,名称为“appServlet”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm absolutely newbie with Java and Spring and I want to learn from example.

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

I'm using an out-of-the-box configuration / installation


  • Mac OSX

  • 作为IDE的Springsource工具套件

  • Spring 2.8.1.RELEASE

  • vfabric-tc-server-developer-2.6.1.RELEASE

  • Mac OSX
  • Springsource Tool Suite as IDE
  • Spring 2.8.1.RELEASE
  • vfabric-tc-server-developer-2.6.1.RELEASE

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

I've tried to generate a new project based on "Spring Template Project". Then I've chosen the "Spring MVC Project". The sample project is generated. After that, without modifying anything, I've tried to execute de "home.jsp" page via "Run As". The Web Server starts and finally I received the error in the console Tab.

这些网页中的其他输出:

And this other output in these web pages:


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

  • http:// localhost:8080 / myproject

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

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

Here you can see an image on how my project is structured (auto generated for STS):

有什么问题?

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

Here you can see the content of the web.xml file:

<?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>

有人有想法解决它吗?

推荐答案

Spring的约定是假设<$ c $> web.xml中的< servlet-name> c> DispatcherServlet 匹配Spring servlet上下文XML文件的开头。将 servlet-context.xml 重命名为 appServlet-context.xml ,看看是否有帮助。

Spring's convention is to assume that the <servlet-name> in the web.xml for the DispatcherServlet matches the beginning of the Spring servlet context XML file. Rename servlet-context.xml to appServlet-context.xml and see if that helps.

这篇关于在DispatcherServlet中找不到带有URI [/ myproject /]的HTTP请求的映射,名称为“appServlet”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:18