【问题标题】:SpringMVC ConfigurationSpringMVC 配置
【发布时间】:2019-05-17 18:27:40
【问题描述】:

我正在尝试配置 spring-mvc 应用程序上下文,但在 JBoss 中运行生成的 war 文件时遇到了一些问题。我收到以下错误 "java.lang.RuntimeException: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/]; 嵌套异常是 java.io.FileNotFoundException: 无法打开 ServletContext 资源"

我似乎无法弄清楚导致错误的原因。我正在运行 springmvc 5.1.6 版。不确定是否需要采取其他措施来解决此问题。

即使按照所示修改了我的配置文件,我仍然收到错误。

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_3_0.xsd"
                   version="3.0">
<display-name>HelloWorld</display-name>

<!-- ===================================================== -->
<!--  1. Create root context with spring listener          -->
<!--     Remove this means only use servlet contxt         -->
<!-- ===================================================== -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- ===================================================== -->
<!-- Can modify default root context config file           -->
<!-- =====================================================

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
-->

<!-- ===================================================== -->
<!--  2. Define servlet with private context               -->
<!-- ===================================================== -->
<servlet>
    <servlet-name>dispatcher-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- ================================================= -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<!-- ===================================================== -->
<!-- One servlet, the dispatcher, to rule it all           -->
<!-- ===================================================== -->
<servlet-mapping>
    <servlet-name>dispatcher-servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

<!-- register controller in servlet private context -->
<context:component-scan base-package="hello.controller"/>

</beans>

应用程序.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.servlet.http.*;

@SpringBootApplication
public class Application extends HttpServlet{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

【问题讨论】:

  • 嗨,您声明的 servlet 为“dispatcher-server”,在 servlet-mapping 中您映射的是“dispatcher”而不是“dispatcher-server”。会不会是这个?

标签: java spring-mvc web.xml


【解决方案1】:

更新: 它看起来像一个错误的配置。您将 servlet 声明为:

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_3_0.xsd"
  version="3.0">
  <display-name>HelloWorld</display-name>

  <!-- ===================================================== -->
  <!--  1. Create root context with spring listener          -->
  <!--     Remove this means only use servlet contxt         -->
  <!-- ===================================================== -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- ===================================================== -->
  <!-- Can modify default root context config file           -->
  <!-- =====================================================

  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  -->

  <!-- ===================================================== -->
  <!--  2. Define servlet with private context               -->
  <!-- ===================================================== -->
  <servlet>
    <servlet-name>dispatcher-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- ================================================= -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- ===================================================== -->
  <!-- One servlet, the dispatcher, to rule it all           -->
  <!-- ===================================================== -->
  <servlet-mapping>
    <servlet-name>dispatcher-servlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

  <!-- register controller in servlet private context -->
  <context:component-scan base-package="hello.controller"/>
</beans>

这是我的控制器:

package hello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {

  @RequestMapping(method = RequestMethod.GET, path = "/hello")
  public String hello() {
    System.out.println("called here");
    return "home";
  }
}

这里是项目结构:

它只是缺少从控制器返回时要显示的页面,但如果您调试,您可以看到它正在调用控制器。

【讨论】:

  • 我修改了我的 web.xml 但仍然没有运气,还有其他想法吗?
猜你喜欢
  • 2016-11-04
  • 2013-11-19
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多