访问SampleSpringMaven原因服务不可用时遇到问题

时间:2014-04-22 15:26:48

标签: java spring maven spring-mvc

开始学习使用Maven的Spring MVC动态Web项目,并使用简单的hello world示例支持Eclipse IDE,但在尝试在服务器上运行时遇到问题。我收到以下错误:

Problem accessing /SampleSpringMaven/. Reason Service Unavailable

这是我的控制台:

在端口8080上启动预览服务器

Modules:
  SampleSpringMaven (null)
  SampleSpringMaven (/SampleSpringMaven)

2014-04-22 11:51:49.441:INFO:oejs.Server:jetty-8.1.10.v20130312
2014-04-22 11:51:49.989:WARN:oejw.WebAppContext:Failed startup of context o.e.j.w.WebAppContext{null,file:/C:/Users/mlim/Eclipse/Test/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/SampleSpringMaven/},C:/Users/mlim/Eclipse/Test/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/SampleSpringMaven
java.lang.IllegalStateException: Null contextPath
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:686)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:454)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
    at org.eclipse.jetty.server.Server.doStart(Server.java:280)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.wst.server.preview.internal.PreviewStarter.run(PreviewStarter.java:72)
    at org.eclipse.wst.server.preview.internal.PreviewStarter.main(PreviewStarter.java:29)
2014-04-22 11:51:50.493:INFO:/SampleSpringMaven:Initializing Spring root WebApplicationContext
Apr 22, 2014 11:51:50 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Apr 22, 2014 11:51:50 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Apr 22 11:51:50 EDT 2014]; root of context hierarchy
Apr 22, 2014 11:51:50 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
Apr 22, 2014 11:51:51 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@29871db1: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0]; root of factory hierarchy
Apr 22, 2014 11:51:51 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 570 ms
2014-04-22 11:51:51.155:INFO:/SampleSpringMaven:Initializing Spring FrameworkServlet 'mvc-dispatcher'
Apr 22, 2014 11:51:51 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'mvc-dispatcher': initialization started
Apr 22, 2014 11:51:51 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'mvc-dispatcher-servlet': startup date [Tue Apr 22 11:51:51 EDT 2014]; parent: Root WebApplicationContext
Apr 22, 2014 11:51:51 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
Apr 22, 2014 11:51:51 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@11846d13: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@29871db1
Apr 22, 2014 11:51:51 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'mvc-dispatcher': initialization completed in 459 ms
2014-04-22 11:51:51.727:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080

和web.xml

<web-app 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"  
    version="2.5">  

    <display-name>Sample Spring Maven Project</display-name>  

    <servlet>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  

    <servlet-mapping>  
        <servlet-name>mvc-dispatcher</servlet-name>  
        <url-pattern>*.htm</url-pattern>  
    </servlet-mapping>  

    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>  
    </context-param>  

    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
</web-app> 

和jsp文件:

<html>  
<head>  
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>  
<title>Being Java Guys | Hello World</title>  
</head>  
<body>  

    <center>  
        <h2>Being Java Guys | Hello World</h2>  
        <h4>${message}</h4>  
    </center>  
</body>  
</html>  

和pom文件:

<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/maven-v4_0_0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <groupId>com.beingjavaguys.sample</groupId>  
    <artifactId>SampleSpringMaven</artifactId>  
    <packaging>war</packaging>  
    <version>1.0-SNAPSHOT</version>  
    <name>SampleSpringMaven Maven Webapp</name>  
    <url>http://maven.apache.org</url>  

    <properties>  
        <spring.version>3.0.5.RELEASE</spring.version>  
        <jdk.version>1.6</jdk.version>  
    </properties>  

    <dependencies>  

        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  

        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  

        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  

        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>3.8.1</version>  
            <scope>test</scope>  
        </dependency>  
    </dependencies>  

    <build>  
        <finalName>SampleSpringMaven</finalName>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.tomcat.maven</groupId>  
                <artifactId>tomcat7-maven-plugin</artifactId>  
                <version>2.1</version>  
                <configuration>  
                    <url>http://localhost:8080/manager/text</url>  
                    <server>my-tomcat</server>  
                    <path>/SampleSpringMaven</path>  
                </configuration>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.0</version>  
                <configuration>  
                    <source>${jdk.version}</source>  
                    <target>${jdk.version}</target>  
                </configuration>  
            </plugin>  
        </plugins>  
    </build>  

</project>  

提前致谢

1 个答案:

答案 0 :(得分:0)

您是否添加了POM文件。有时,无法正确下载回购,您可能需要删除现有的回购并更新maven项目以再次下载dependecies。

这是您可以开始使用的基本POM。

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.beingjavaguys.sample</groupId>
    <artifactId>SampleSpringMaven</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SampleSpringMaven Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
        <jdk.version>1.6</jdk.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>SampleSpringMaven</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>my-tomcat</server>
                    <path>/SampleSpringMaven</path>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

你也可以看一下它有开始使用Spring的基本内容。

http://www.beingjavaguys.com/2013/08/spring-maven-web-application-in-eclipse.html
相关问题