SpringMVC配置

时间:2019-05-17 18:27:41

标签: java spring-mvc web.xml

我正在尝试配置spring-mvc应用程序上下文,但是在JBoss中运行生成的war文件时遇到一些问题。我收到以下错误 “ java.lang.RuntimeException:org.springframework.beans.factory.BeanDefinitionStoreException:IOException从ServletContext资源解析XML文档[/];嵌套异常是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>

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

application.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);
    }

}

1 个答案:

答案 0 :(得分:0)

已更新: 看起来像是配置错误。您将该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>

dispatcher-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";
  }
}

这里是项目结构:

Project structure

仅丢失了要从控制器返回时要显示的页面,但是如果您进行调试,则可以看到它正在调用控制器。