1

I am new to spring MVC and maven also. I created a maven web project in eclipse. Add dependencies for spring and run the project, but i am not getting the desired result. Here is my project structure

Project Structure in eclipse

When i run the project then i get the result Hello World which is my 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>

But when i change to url http://localhost:8080/Spring_Maven/jsp/hello i get HTTP Status 500 error. And when i change to url http://localhost:8080/Spring_Maven/jsp/hello.jsp then i get the output ${message}

Here is my hello.jsp page

<!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>

Here is my 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>

here is my 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>

Here are the list of dependencies that i added through maven

spring dependencies

What i am doing wrong?

4

1 回答 1

1

最后我让它工作了:)。但我想分享一下,以便那些刚接触 Maven 和 Eclipse 的人可以节省他们的时间。

首先,我安装了m2e eclipse WTP pluginmaven 项目,然后按照我在问题中的描述创建了 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 移动到其中。

我的项目的最终结构

虽然没有 lib 文件夹,WEB-INF但一切正常。我花了这么多时间的事情是同时定义servletcontext paramservelet init-param。如果我只定义 servlet init 参数

<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带有 url的页面http://localhost:8080/SpringMavenHelloWorld/,当我将其更改为时,http://localhost:8080/SpringMavenHelloWorld/hello我得到我想要的输出,即

在此处输入图像描述

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

于 2013-05-09T12:30:16.337 回答