spring - Maven Tomcat 嵌入式

标签 spring tomcat maven-2

我正在尝试使用 maven 运行 Spring webapp

mvn tomcat:run

命令,但每当我导航到 http://localhost:8080/myApp 时,我都会收到错误消息:“请求的资源 () 不可用”。日志中没有显示任何内容。我认为我的应用程序应该部署到“/”而不是“/myApp”。有办法做到这一点吗?

Maven输出日志:

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'tomcat'.
[INFO] ------------------------------------------------------------------------
[INFO] Building myApp
[INFO]    task-segment: [tomcat:run]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing tomcat:run
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
Downloading: http://svn.asdf.com:8081/artifactory/repo/org/postgresql/postgresql/8.3/postgresql-8.3.pom
[INFO] Unable to find resource 'org.postgresql:postgresql:pom:8.3' in repository central (http://repo1.maven.org/maven2)
Downloading: http://svn.asdf.com:8081/artifactory/repo/java-memcached/java-memcached/1.6/java-memcached-1.6.pom
[INFO] Unable to find resource 'java-memcached:java-memcached:pom:1.6' in repository central (http://repo1.maven.org/maven2)
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [tomcat:run {execution: default-cli}]
[INFO] Running war on http://localhost:8080/myApp
[INFO] Using existing Tomcat server configuration at C:\cygwin\home\workspace\myApp\target\tomcat
Sep 4, 2009 3:17:29 PM org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
Sep 4, 2009 3:17:29 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.16
Sep 4, 2009 3:17:30 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
Sep 4, 2009 3:17:31 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'myApp'
Sep 4, 2009 3:17:31 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Sep 4, 2009 3:17:31 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080

网络.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
                /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>

    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>myApp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>3</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>myApp</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

最佳答案

maven tomcat plugin 的文档这里说 默认上下文路径为 /${project.build.finalName},它本身默认为 /${artifactId}-${version}

更改上下文路径的首选方法是通过将以下内容添加到 pom.xml 来更改 finalName:

<build>
        <finalName>mycontext</finalName>
</build>

或者,可以通过向 pom.xml 添加显式插件配置 block 来指定不同的上下文路径,如下所示:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
                <path>/mycontext</path>
        </configuration>
</plugin>

如果您在 pom 中定义了这两个元素中的任何一个,那么它应该加载到 localhost:8080/wherever。否则它将位于 localhost:8080/${artifactId}-${version}

关于spring - Maven Tomcat 嵌入式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1380741/

相关文章:

java - Spring MVC从子菜单调用jsp

spring - spring中的多步登录页面

Java Spring Data @Autowired 问题

java - validateJarFile(servlet-api.jar) - jar 未使用 eclipse 加载到 tomcat 中

maven-2 - Maven Release Plugin部署sources.jar和javadoc.jar

java - Spring 无法定位 OSGi 命名空间处理程序

java - 如何同时使用 Tomcat 的非阻塞连接器(NIO 或 APR)和 Apache Httpd?

spring - 每个请求模型的事件循环和线程之间的区别

maven-2 - 如何将 httpclient 4.1.x 与 Maven 一起使用

java - Eclipse(STS)/Maven集成问题