2013-05-08 47 views
1

我是Spring MVC和Maven的新手。我在eclipse中創建了一個maven web項目。爲spring添加依賴關係並運行該項目,但我沒有得到期望的結果。這裏是我的項目結構Spring MVC未顯示預期結果

Project Structure in eclipse

當我運行該項目,然後我得到的結果Hello World這是我的index.jsp

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <title>Hello</title> 
    </head> 
    <body> 
     <h1>Hello World</h1> 
    </body> 
</html> 

但是,當我改變URL http://localhost:8080/Spring_Maven/jsp/hello我得到HTTP Status 500 error。當我改變URL http://localhost:8080/Spring_Maven/jsp/hello.jsp然後我得到的輸出${message}

,這裏是我的hello.jsp頁面

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
     <title>Hello</title> 
    </head> 
    <body> 
     <h1>${message}</h1> 
    </body> 
</html> 

這裏是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="WebApp_ID" version="2.4" 
    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"> 

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

    <servlet-mapping> 
     <servlet-name>springMaven-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

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

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

</web-app> 

這裏是我的springMaven,調度員-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    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-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

    <context:component-scan base-package="pk.training.basitMahmood.springMaven.controller" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/jsp/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
</beans> 

這裏是依賴項列表tha T I基於Maven

spring dependencies

添加什麼,我做錯了什麼?

+0

我在搜索過程中發現的一件事是,maven依賴項應該複製到'WEB-INF/lib'文件夾中。但它不是複製。雖然當我去'項目屬性 - >部署大會'我有'Meven依賴映射到WEB-INF/lib'。但是我的WEB-INF中沒有lib文件夾。我還不知道是否需要手動創建該文件夾。我也嘗試通過手動創建文件夾,但罐子不復制到lib文件夾不知道爲什麼......但這是我的發現,直到:) – Basit 2013-05-09 10:56:07

回答

1

最後我讓它工作:)。但我想分享,所以新的maven和eclipse的人可以節省他們的時間。

首先我安裝了m2e eclipse WTP plugin,然後創建maven項目,正如我在我的問題中所述。你需要做的是在你的pom.xml文件中添加編譯器插件和JDK版本,否則每次你做right click on project --> Maven --> Update project你都會在標記選項卡中看到關於JRE and java EE configuration problem的錯誤。您還需要做right click on project --> properties --> Project facets --> Change java version以改變項目方面。這裏是pom.xml的片段。

<build> 
    <finalName>SpringMavenHelloWorld</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

然後在web.xml我更新的servelt模式,我發現我需要定義兩個調度員servlet和在servlet上下文我SERVET-dispatcer.xml文件。這是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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_3_0.xsd" 
    version="3.0"> 
    <display-name>Spring_Maven</display-name> 
    <servlet> 
     <servlet-name>springMaven-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
     <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>springMaven-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

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

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

    <welcome-file-list> 
      <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

</web-app> 

這是我的項目的結構。我改變了一下。在WEB-INF中創建一個spring文件夾,並將調度器servlet移入其中。

Final structure of my project

雖然在WEB-INF沒有lib文件夾,但一切工作正常。我花了太多時間的是定義了servletcontext paramservelet init-param。如果我只有這樣定義

<servlet> 
    <servlet-name>springMaven-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>springMaven-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<!-- 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value> 
</context-param> 
--> 

然後servlet的初始參數我得到了錯誤

SEVERE: Context initialization failed 
org.springframework.beans.factory.BeanDefinitionStoreException: 
IOException parsing XML document from ServletContext resource 
[/WEB-INF/applicationContext.xml]; nested exception is 
java.io.FileNotFoundException: Could not open ServletContext 
resource [/WEB-INF/applicationContext.xml] 

,如果我只定義背景PARAM像

<servlet> 
    <servlet-name>springMaven-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <!-- 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/springMaven-dispatcher-servlet.xml</param-value> 
    </init-param> 
    --> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>springMaven-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

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

然後我得到了錯誤

SEVERE: Context initialization failed 
org.springframework.beans.factory.BeanDefinitionStoreException: 
IOException parsing XML document from ServletContext resource 
[/WEB-INF/springMaven-dispatcher-servlet.xml]; nested exception is 
java.io.FileNotFoundException: Could not open ServletContext resource 
[/WEB-INF/springMaven-dispatcher-servlet.xml] 

但是def兩者都解決了這個問題。現在,當我做right click on my project --> run on server然後我得到Hello World的頁面URL http://localhost:8080/SpringMavenHelloWorld/,當我將其更改爲http://localhost:8080/SpringMavenHelloWorld/hello然後我得到我想要的輸出是

enter image description here

希望這將幫助其他人了。謝謝:)