首页 文章

HTTP请求未被委托给根WebApplicationContext的控制器

提问于
浏览
1

我正在关注Spring Docs并配置 web.xmlapplicatioinContext.xml 使用功能 Single root context in 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>

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"
    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:

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

所以当我在浏览器中点击URL http://localhost:8080/PracticeJavaWeb/controller时,它显示404

Server log saying:

警告:在DispatcherServlet中找不到具有URI [/ PracticeJavaWeb / controller]的HTTP请求的映射,其名称为“dispatcher”

Spring Docs saying:

对于单个DispatcherServlet方案,也可以只有一个根上下文 .

2 回答

  • 1

    只需对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>
    
  • 3

    您需要在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>
    

相关问题