2014-04-23 119 views
0

我一直在檢查幾個教程和其他幫助,這裏在stackoverflow,但我無法解決這個惱人的問題:我在Eclipse中創建了一個Spring的MVC項目後,從一步到另一個this教程我仍然從Tomcat服務器收到「找不到資源」錯誤。Spring MVC教程不起作用

WEB-INF/web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>onlab</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>onlab</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

WEB-INF/onlab-servlet.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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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"> 


    <context:component-scan base-package="controller" /> 

    <mvc:annotation-driven /> 

</beans> 

SRC /控制器/ HomeControllerJava:

@Controller 
public class HomeController { 

    @RequestMapping(value = "/") 
    public String home() { 
     System.out.println("HomeController: Passing through..."); 
     return "home"; 
    } 
} 

WEB -INF/views/home.jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ page session="false" %> 
<html> 
    <head> 
     <title>Home</title> 
    </head> 
    <body> 
     <h1>Hello world!</h1> 
    </body> 
</html> 

最後的src /控制器/ Configurer.java:

@Configuration 
public class Configurer { 
    @Bean 
    ViewResolver viewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setPrefix("WEB-INF/views/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 

上次發生這種相同的教程工作很適合我,但現在我打的時候老是不從Apache找到資源。

+0

您將請求發送到哪個URL? –

+0

這是一個錯誤:或者你打電話給你的文件夾WEB-ING /?它可能會解決您的問題,重命名它WEB-INF – Tony

+0

整個堆棧跟蹤將很好 – cbach

回答

0

因爲我不能添加評論我要去的答案:)。

不知道你的Tomcat/IDE設置,這是我的假設,我怎麼得出最終的結論是: - 你的代碼不會返回錯誤 -The IDE是建立在正確的位置 部署文件 - 您有Tomcat設置正確地使用

由於您在Eclipse中運行(如果您使用它,那麼您可以使用Spring Tools Suite,因爲它專門爲Spring設計),您應該配置Tomcat服務器。 我以前的經驗是Tomcat中的映射(它也在http.conf文件中)使用了不同的前綴,然後我期望由於其他衝突項目。

在這種情況下,當您假設其localhost:8080/onlab /時,它可能被配置爲在localhost:8080 /甚至localhost:8080 // onlab /上查找相同的文件。

檢查出你的tomcat配置,並確保它指向正確的位置。