2014-04-23 51 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配置,并确保它指向正确的位置。