HTTP请求未被委派给根WebApplicationContext

时间:2017-09-08 00:57:43

标签: java spring spring-mvc

我正在关注此Spring Docs并配置web.xmlapplicatioinContext.xml以使用功能 Spring Web MVC中的单根上下文 即没有Servlet WebApplicationContext;所有请求都将由Root WebApplicatioinContext提供。

但问题是HTTP请求未被委托给根WebApplicationContext的控制器。

的web.xml:

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/root-context.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

根context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="PostController" class="org.my.controllers.PostController">
    </bean>

</beans>

控制器:

@Controller
public class PostController {
    @RequestMapping(value= "/controller", method = RequestMethod.GET)
    @ResponseBody
    public String foo() {
        return "Response!";
    }
}

因此,当我在浏览器中点击网址http://localhost:8080/PracticeJavaWeb/controller时,它显示404

服务器日志说:

  

警告:找不到带URI的HTTP请求的映射   [/ PracticeJavaWeb / controller]在DispatcherServlet中的名称   &#39;调度&#39;

Spring Docs说:

  

也可以只有一个根上下文用于单个   DispatcherServlet场景。

2 个答案:

答案 0 :(得分:3)

对root-context.xml文件只做一个小改动,添加:

<mvc:annotation-driven></mvc:annotation-driven>

mvc:annotation-driven使上下文路径能够识别spring mvc注释,以便将请求分派给控制器。

这里还有一个root-context.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        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-4.3.xsd">


    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="welcomeService" class="com.myorg.controller.MyService"></bean>
    <bean id="welcomControler" class="com.myorg.controller.WelcomeController"></bean>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

答案 1 :(得分:1)

您需要在root-context.xml中添加<mvc:annotation-driven />标记。

<mvc:annotation-driven />标记可确保将您的请求分派给控制器。

并且不要忘记在root-context.xml中添加mvc名称空间

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>