【问题标题】:Application not workig when adding value to <context-param> in web.xml in Spring MVC Web Application Using Maven在使用 Maven 的 Spring MVC Web 应用程序中向 web.xml 中的 <context-param> 添加值时应用程序无法正常工作
【发布时间】:2014-01-22 01:02:17
【问题描述】:

我正在使用 Spring Mvc 创建一个 Web 应用程序。

当我没有为 web.xml 中的 context-param 提供价值时,我正面临这样的问题 然后 index page 能够显示,但是当我在 web.xml 中为 context-param 提供值时,然后 index page没有显示。

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>Core Web Application</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>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
            <welcome-file>/WEB-INF/pages/index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        </param-value>
    </context-param>

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

当我尝试 localhost:8080/CoreWebApp/ 我得到 index.jsp

web.xml :- 当我为 context-param 提供价值时

<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>Core Web Application</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>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
            <welcome-file>/WEB-INF/pages/index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
                    /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>

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

当我尝试 localhost:8080/CoreWebApp/ 时,我没有得到 index.jsp。

Please help me to find out the problem.

My Project Structure is as :-
 CoreWebApp
   |_______________ pom.xml
   |
   |_______________ src
   |                 |_____________ main
   |                                 |___________ java
   |                                 |___________ resources
   |                                 |___________ webapp
   |                                                |____ WEB-INF
   |                                                        |_____ applicationContext.xml
   |                                                        |
   |                                                        |_____ mvc-dispatcher-servlet.xml
   |                                                        |
   |                                                        |_____ web.xml
   |                                                        |
   |                                                        |_____ pages
   |                                                                |________ index.jsp
   |
   |______________ target

【问题讨论】:

    标签: xml spring maven spring-mvc


    【解决方案1】:

    你的 spring Servlet 有一个“/”的映射 以及索引页的路径。

    【讨论】:

      【解决方案2】:

      检查日志中的一些异常并将它们添加到问题中。 Spring MVC 还搜索带有名称的 xml 配置:"&lt;the name of the dispatcher servlet&gt;+-servlet" 尝试将applicationContext.xml 重命名为mvc-dispatcher-servlet.xml 并相应地更改路径。

      【讨论】:

      • Hey Evgeni,这不是 mvc-dispatcher-servlet.xml 的问题。由于 Spring Mvc 约定,spring 框架寻找 -servlet.xml。在我的 web.xml 中,在 处,servlet-name 是 "mvc-dispatcher" 并且我有 mvc -dispatcher-sevlet.xml 在“WEB-INF”文件夹下
      • 好的。日志中有异常吗?
      猜你喜欢
      • 2019-12-15
      • 2014-05-08
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多