【问题标题】:HTTP requests are not being delegated to controllers of root WebApplicationContextHTTP 请求未委托给根 WebApplicationContext 的控制器
【发布时间】:2018-02-16 19:50:38
【问题描述】:

我正在关注 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>

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
public class PostController {
    @RequestMapping(value= "/controller", method = RequestMethod.GET)
    @ResponseBody
    public String foo() {
        return "Response!";
    }
}

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

服务器日志说:

警告:找不到带有 URI 的 HTTP 请求的映射 DispatcherServlet 中的 [/PracticeJavaWeb/controller] 与名称 '调度员'

Spring Docs 说:

也可能只有一个根上下文 DispatcherServlet 场景。

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      您需要在root-context.xml中添加&lt;mvc:annotation-driven /&gt;标签。

      &lt;mvc:annotation-driven /&gt; 标签确保您的请求被分派到控制器。

      别忘了在 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-23
        • 1970-01-01
        • 2018-01-01
        • 2011-12-28
        • 1970-01-01
        • 2015-04-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多