Spring MVC没有显示出预期的结果

时间:2013-05-08 10:40:51

标签: spring-mvc

我是新手MVC和maven。我在eclipse中创建了一个maven web项目。为spring添加依赖项并运行项目,但我没有得到所需的结果。这是我的项目结构

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>

但是当我更改为网址http://localhost:8080/Spring_Maven/jsp/hello时,我得到HTTP Status 500 error。当我更改为网址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-dispatcher-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>

以下是我通过maven添加的依赖项列表

spring dependencies

我做错了什么?

1 个答案:

答案 0 :(得分: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模式,我发现我需要在dispatcher-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 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>
-->

然后我收到错误

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]

如果我只定义像

这样的上下文参数
<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]

但定义两者都可以解决问题。现在,当我right click on my project --> run on server时,我会使用网址Hello World获取页面http://localhost:8080/SpringMavenHelloWorld/,当我将其更改为http://localhost:8080/SpringMavenHelloWorld/hello时,我会获得所需的输出

enter image description here

希望这也有助于其他人。谢谢:))