[英]Running a Spring MVC application in Tomcat
我想測試我在春季使用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>
我發現有趣的是,盡管Servlet名稱是appServlet,但它在STS中可以與“ http:// localhost:8080 / mvc / ”一起使用; 我也嘗試使用appServlet,但是遇到了同樣的錯誤。
在web-inf目錄中創建了appServlet-servlet.xml嗎?
在初始化DispatcherServlet時,Spring MVC在WEB-INF目錄中查找名為[servlet-name] -servlet.xml的文件。
順便說一句,如果您要創建Restful Web Service的測試項目,則可以簡單地在Google中搜索“ Spring Rest Maven休眠示例”,然后將獲得一堆示例參考和zip文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.