简体   繁体   English

Spring MVC应用程序不适用于Tomcat和Eclipse

[英]Spring MVC application don't work with Tomcat and Eclipse

I'm having a lot of trouble trying to use Spring Framework and a don't know where is the error. 我在尝试使用Spring Framework时遇到很多麻烦,而且不知道错误在哪里。 I'm deploying the application using Tomcat 7.0.68. 我正在使用Tomcat 7.0.68部署应用程序。

When I try to use the URL ( http://localhost:8080/fj21-spring/olaMundo ) I'm getting a 404 error, but its should be mapped to call my Action method on my Controller class, as below: 当我尝试使用URL( http:// localhost:8080 / fj21-spring / olaMundo )时,出现404错误,但应映射为在Controller类上调用Action方法,如下所示:

package com.caelum.tarefas.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TarefasController {

  @RequestMapping("/olaMundo")
  public String execute(){
      return "ok";
  }
}

My pom.xml file is what follows: 我的pom.xml文件如下:

 <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.caelum.tarefas</groupId> <artifactId>fj21-spring</artifactId> <version>0.0.1-SNAPSHOT</version> <name>fj21-spring</name> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.19</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.19</version> </dependency> </dependencies> </project> 

And my web.xml: 而我的web.xml:

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>fj21-spring</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 

My Spring XML configuration file: 我的Spring XML配置文件:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.caelum.tarefas" /> <mvc:annotation-driven></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans> 

I expect that someone could help me. 我希望有人可以帮助我。 I had tryed to recreated the Server in Eclipse, deleted the project and create another one, but until now nothing worked. 我曾尝试在Eclipse中重新创建服务器,删除了该项目并创建了另一个项目,但是直到现在仍然没有任何效果。

Thank you! 谢谢!

EDIT 编辑

After change my annotation on Controller class to @RequestMapping(value="/olaMundo"), the Tomcat write a stack trace on console, which follows (maybe I missed this stack trace before too): 将我在Controller类上的注释更改为@RequestMapping(value =“ / olaMundo”)后,Tomcat在控制台上编写了堆栈跟踪,如下所示(也许我之前也错过了此堆栈跟踪):

GRAVE: Servlet [spring] in web application [/fj21-spring] threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:504)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:486)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:113)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1146)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1085)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5318)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5610)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

It looks like the application your deployed can't find the spring libs enter link description here 您部署的应用程序似乎找不到spring libs,请在此处输入链接描述

may this question can help you 请问这个问题可以帮助您

Is the jsp you are trying to display called "ok.jsp"? 您要显示的jsp是“ ok.jsp”吗? If not this is what your controller is looking for in your views folder when it returns "ok". 如果不是,这就是控制器返回“确定”时正在视图文件夹中寻找的内容。

Try replacing your @RequestMapping("/olaMundo") with @RequestMapping(value="/olaMundo") 尝试将@RequestMapping("/olaMundo")替换为@RequestMapping(value="/olaMundo")

I see ClassNotFoundException. 我看到ClassNotFoundException。 check the jar file in lib dir and check the configuration like this. 检查lib dir中的jar文件,并检查像这样的配置。 properties --> deployment assembly 属性->部署程序集 在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM