简体   繁体   English

Spring Dispatcher servlet找不到index.html。 在DispatcherServlet中找不到带有URI []的HTTP请求的映射

[英]Spring dispatcher servlet cannot find index.html . No mapping found for HTTP request with URI [] in DispatcherServlet

I've been trying to get a simple spring mvc webapp working. 我一直在尝试使一个简单的spring mvc webapp工作。 I've more or less followed this tutorial: http://www.codejava.net/frameworks/spring/spring-mvc-beginner-tutorial-with-spring-tool-suite-ide 我或多或少地遵循了本教程: http : //www.codejava.net/frameworks/spring/spring-mvc-beginner-tutorial-with-spring-tool-suite-ide

Please note that im using spring 3.2.5.RELEASE. 请注意,即时通讯使用的是Spring 3.2.5.RELEASE。 Everything is working fine except when the controller returns "index", the dispatcher servlet maps to "/websitebasetest/WEB-INF/views/index.html" which is not found apparently even though there is a file with that name in "WEB-INF/views". 一切工作正常,除非控制器返回“索引”,调度程序servlet映射到“ /websitebasetest/WEB-INF/views/index.html”,即使在“ WEB- INF /意见”。

The controller is called which is great! 控制器被称为很棒! But afterwards there seems to be an issue finding the related view which is weird because the log suggests that the view resolver found the appropriate view in "/websitebasetest/WEB-INF/views/index.html"(see below). 但是此后,发现相关视图似乎很奇怪,因为日志表明视图解析器在“ /websitebasetest/WEB-INF/views/index.html”中找到了合适的视图(见下文)。

My question is, why is it not finding the view(displays 404 instead) and how do I get it to work? 我的问题是,为什么找不到视图(改为显示404),如何使它起作用?

Here is information you may ask for: 这是您可能需要的信息:

Project structure: project-structure.png . 项目结构: project-structure.png

First half of pom.xml (I can post the rest upon request) : pom.xml的前半部分(我可以应要求发布其余部分):

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tenacious</groupId>
<artifactId>websitebasetest</artifactId>
<name>websitebasetest</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>

<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.2.5.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.10</org.aspectj-version>
    <org.slf4j-version>1.6.6</org.slf4j-version>
</properties>

<dependencies>
    <!-- Spring core dependency-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>

    <!-- Spring MVC dependency-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

root-context.xml: 根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.xsd">

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

servlet-context.xml: 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.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

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

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

<!-- Handles HTTP GET requests for "/css/**" and "/js/**" by efficiently serving up 
     static resources in the ${webappRoot}/css and the ${webappRoot}/js directories respectively -->
<resources mapping="/css/**" location="/WEB-INF/css" />
<resources mapping="/js/**" location="/WEB-INF/js" />

<!-- Resolves views selected for rendering by @Controllers to .html 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=".html" />
</beans:bean>

<context:component-scan base-package="com.tenacious.websitebasetest" />

</beans:beans>

web.xml: 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>

HomeController.java: HomeController.java:

package com.tenacious.websitebasetest;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application "home" page.
 */
@Controller
public class HomeController 
{   
    private static final Logger logger =     LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Locale locale, Model model) 
    {
        logger.info("Hello world!");

        return "index";
    }
}

index.html: index.html的:

<html>
<head>
    <title>Home</title>
</head>
<body>
    <h1>Hello world!</h1>
</body>
</html>

URL I am trying to reach: localhost:8080/websitebasetest/ 我尝试访问的URL:localhost:8080 / websitebasetest /

console log: 控制台日志:

Aug 14, 2016 2:28:49 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 690 ms
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sun Aug 14 14:28:50 EDT 2016]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/root-context.xml]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5792f62e: defining beans []; root of factory hierarchy
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 199 ms
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Sun Aug 14 14:28:50 EDT 2016]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@67b7829a: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#1,org.springframework.web.servlet.view.InternalResourceViewResolver#0,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@5792f62e
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.tenacious.websitebasetest.HomeController.index(java.util.Locale,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped   URL path [/css/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/js/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 523 ms
Aug 14, 2016 2:28:51 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1665 ms
INFO : com.tenacious.websitebasetest.HomeController - Hello world!
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/websitebasetest/WEB-INF/views/index.html] in     DispatcherServlet with name 'appServlet'

404 page: 404页:

HTTP Status 404 - HTTP状态404-

type Status report 类型状态报告

message 信息

description The requested resource is not available. 描述所请求的资源不可用。

Pivotal tc Runtime 3.1.2.RELEASE/8.0.26.B.RELEASE Pivotal tc运行时3.1.2.RELEASE / 8.0.26.B.RELEASE

Any help would be greatly appreciated. 任何帮助将不胜感激。 thank you. 谢谢。

I just ran your entire code in the my local machine just to see if my way is correct or not. 我只是在本地计算机上运行了整个代码,只是想看看我的方式是否正确。 The problem is in your web.xml 问题出在您的web.xml中

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

Instead of this, It should be 代替这个,应该是

    <servlet-mapping>
           <servlet-name>appServlet</servlet-name>
           <url-pattern>*.html</url-pattern>
    </servlet-mapping>

This way your servlet can handle anything with the .html extension. 这样,您的servlet就可以处理.html扩展名的任何内容。

BTW i ran it in jetty server 顺便说一句,我在码头服务器上运行它

I have found a solution. 我找到了解决方案。

In servlet context i added: 在servlet上下文中,我添加了:

<resources mapping="/views/**" location="/WEB-INF/views/" />

and edited this part: 并编辑了这一部分:

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

The view resolver receives "index" from the controller which then changes it to "/views/index.html". 视图解析器从控制器接收“索引”,然后将其更改为“ /views/index.html”。 Since I added a resource mapping for anything starting with "/views/**" to map to "/WEB-INF/views/" it now finds the view correctly xD. 由于我添加了以“ / views / **”开头的任何内容的资源映射以映射到“ / WEB-INF / views /”,因此它现在可以正确地找到xD视图。

It seems that being mapped to "/websitebasetest/WEB-INF/views/index.html" (which is what it was doing before) was incorrect as it could not locate the file. 似乎被映射到“ /websitebasetest/WEB-INF/views/index.html”(这是之前所做的)是错误的,因为无法找到文件。

Special thanks to people that helped along the way!! 特别感谢在此过程中提供帮助的人们!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Spring MVC中在名称为&#39;dispatcher&#39;的DispatcherServlet中找不到带有URI的HTTP请求的映射... - No mapping found for HTTP request with URI …in DispatcherServlet with name 'dispatcher' in spring mvc 春季:在DispatcherServlet中,名称为“ dispatcher”的URI []未找到HTTP请求的映射 - Spring: No mapping found for HTTP request with URI [] in DispatcherServlet with name 'dispatcher' 春季:在Dispatcher Servlet中找不到带有URI [uri]的HTTP请求的映射 - Spring: No mapping found for HTTP request with URI [uri] in Dispatcher Servlet Spring MVC:HTTP状态404,在名称为“ spring-dispatcher”的DispatcherServlet中找不到带有URI [/ BugzillaReport /]的HTTP请求的映射 - Spring MVC: HTTP Status 404, No mapping found for HTTP request with URI [/BugzillaReport/] in DispatcherServlet with name 'spring-dispatcher' 在名称为&#39;dispatcher&#39;的DispatcherServlet中找不到带有URI的HTTP请求的映射”“ - No mapping found for HTTP request with URI in DispatcherServlet with name 'dispatcher'" 在名称为&#39;dispatcher&#39;的DispatcherServlet中找不到HTTP请求URI [js&css]的映射 - No mapping found for HTTP request URI[js & css] in DispatcherServlet with name 'dispatcher' 在名称为&#39;dispatcher&#39;的DispatcherServlet中找不到带有URI []的HTTP请求的映射 - No mapping found for HTTP request with URI [] in DispatcherServlet with name 'dispatcher' 警告:在名称为“dispatcher”的 DispatcherServlet 中未找到具有 URI [/Test/] 的 HTTP 请求的映射 - WARNING: No mapping found for HTTP request with URI [/Test/] in DispatcherServlet with name 'dispatcher' 警告:在名称为&#39;dispatcher&#39;的DispatcherServlet中找不到带有URI [/ SpringMVCpractice /]的HTTP请求的映射 - WARNING: No mapping found for HTTP request with URI [/SpringMVCpractice/] in DispatcherServlet with name 'dispatcher' 在名称为&#39;dispatcher&#39;的DispatcherServlet中找不到带有URI [/ MVCCrud / viewemp]的HTTP请求的映射 - No mapping found for HTTP request with URI [/MVCCrud/viewemp] in DispatcherServlet with name 'dispatcher'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM