2016-08-14 124 views
1

我一直在試圖獲得一個簡單的spring mvc webapp工作。我或多或少地遵循本教程:http://www.codejava.net/frameworks/spring/spring-mvc-beginner-tutorial-with-spring-tool-suite-ideSpring調度程序servlet找不到index.html。在DispatcherServlet中找不到使用URI []的HTTP請求的映射

請注意,即時通訊使用彈簧3.2.5.RELEASE。除非控制器返回「index」,否則一切都工作正常,調度程序servlet映射到「/websitebasetest/WEB-INF/views/index.html」,即使在「WEB- INF /意見」。

控制器被稱爲是偉大的!但之後,似乎有一個問題發現相關的視圖很奇怪,因爲日誌建議視圖解析器在「/websitebasetest/WEB-INF/views/index.html」(見下文)中找到適當的視圖。

我的問題是,爲什麼它沒有找到視圖(而是顯示404),我如何得到它的工作?

以下是你可能會問的信息:

項目結構: project-structure.png

首先pom.xml中的一半(I可以張貼根據要求的其餘部分):

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

根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中:

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

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

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:我想

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

URL來訪問:本地主機:8080/websitebasetest/

控制檯日誌:

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.s[email protected]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.s[email protected]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.s[email protected]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頁數:

HTTP狀態404 -

類型狀態報告

消息

描述所請求的資源不可用。

樞紐TC運行3.1.2.RELEASE/8.0.26.B.RELEASE

任何幫助將不勝感激。謝謝。

回答

1

我找到了解決方案。

在servlet上下文我增加:

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

和編輯這一部分:

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

的視圖解析器從控制器,其然後改變到「/視圖/索引接收 「索引」。 HTML」。由於我爲任何以「/ views/**」開頭的映射到「/ WEB-INF/views /」的資源添加了資源映射,因此它現在可以正確地找到視圖xD。

似乎被映射到「/websitebasetest/WEB-INF/views/index.html」(這是之前做的)是不正確的,因爲它無法找到該文件。

特別感謝一路上幫助過的人!

0

我剛剛在我的本地機器上運行了您的整個代碼,以查看我的方式是否正確。 問題出在您的網頁上。XML

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

取而代之的是,它應該是

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

這樣,你的servlet可以處理與.html擴展任何東西。

順便說一句我運行它在碼頭服務器

+0

這沒有奏效。事實上,當我嘗試這個「Hello World」不會出現在控制檯日誌中。這意味着HomeController.index()沒有被調用... – TenaciousDan

+0

你可以共享一些控制檯日誌嗎?我只是在本地進行了測試,並且工作正常。\ – Sohil

+0

@TenaciousDan我在碼頭服務器上運行的其他修改是失去了所有slf4j依賴關係,並且失去了常見的日誌記錄排除功能。看看它是否適用於您。 – Sohil