【问题标题】:Running a Spring MVC application in Tomcat在 Tomcat 中运行 Spring MVC 应用程序
【发布时间】:2017-01-04 01:40:41
【问题描述】:

我想测试我在春季使用 Tomcat 8 创建的 REST Web 服务。在继续之前,我想验证我是否可以在 Tomcat 中运行 MVC 应用程序,但是我遇到了问题;在 STS 中,应用程序被称为“http://localhost:8080/mvc/”。我导出了一个 war 文件并将其部署在 Tomcat 中(我什至可以在 webapps 文件夹中看到它),但出现 404 错误。我做了一些修改;我加了

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>

到 pom.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>

我觉得有趣的是,在 STS 中它可以与“http://localhost:8080/mvc/”一起使用,尽管 servlet 名称是 appServlet;我也尝试了 appServlet,但遇到了同样的错误。

【问题讨论】:

  • 您收到 404 错误的 URL 是什么?
  • STS 是 IDE,而不是应用服务器。从 STS 运行应用程序时,您在哪里部署应用程序?我的猜测是它是Tomcat。你的战争文件的名称是什么?
  • 在 tomcat 6 中,我记得有一个配置文件,您需要在其中使用 autoDeploy="true" 等参数指定主机名配置。我不知道 tomcat 8,但很有可能您必须在里面这样做您的容器,或者直接从您的 Maven 插件中找到所需的选项来配置它。
  • 嗯,你的maven插件没问题,你不能直接处理,考虑使用cargo插件codehaus-cargo.github.io/cargo/Home.html

标签: java web-services rest spring-mvc tomcat8


【解决方案1】:

您是否在 web-inf 目录中创建了 appServlet-servlet.xml?

在初始化 DispatcherServlet 时,Spring MVC 在 WEB-INF 目录中查找名为 [servlet-name]-servlet.xml 的文件

顺便说一句,如果你想创建一个Restful Web Service的测试项目,你可以简单地谷歌“Spring Rest Maven hibernate sample”,你会得到一堆示例参考和zip文件。

【讨论】:

    猜你喜欢
    • 2013-05-11
    • 2023-03-16
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2018-05-13
    相关资源
    最近更新 更多