简体   繁体   中英

SpringMVC Configuration

I am trying to configure spring-mvc application context but I having some issues running the generated war file in JBoss. I get the following error "java.lang.RuntimeException: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource"

I can't seem to figure out what is happening to cause the error. I am running springmvc version 5.1.6. Not sure if there is something additional that needs to happen to fix this.

Even after modifying my configuration files as shown, I still get the error.

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

}

UPDATED: It looks like a misconfiguration. You declared the servlet as:

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>

Here is my Controller:

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

And here, the project structure:

项目结构

It is only missing the page you want to display when returning from the controller but if you debbug you can see it calling the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM