有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java HTTP请求未委派给根WebApplicationContext的控制器

我遵循这个Spring Docs并将web.xmlapplicatioinContext.xml配置为在Spring Web MVC中使用功能单根上下文,即没有Servlet WebApplicationContext;所有请求都将由根WebApplicationContext提供

但问题是HTTP请求并没有被委派给根WebApplicationContext的控制器

网络。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>

根上下文。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!";
    }
}

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

服务器日志显示:

WARNING: No mapping found for HTTP request with URI [/PracticeJavaWeb/controller] in DispatcherServlet with name 'dispatcher'

Spring Docs说:

It is also possible to have just one root context for single DispatcherServlet scenarios.


共 (2) 个答案

  1. # 1 楼答案

    您需要在根上下文中添加<mvc:annotation-driven />标记。xml

    <mvc:annotation-driven />标记确保您的请求被发送到控制器

    不要忘记在根上下文中添加mvc名称空间。xml

    <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>
    
  2. # 2 楼答案

    只需对根上下文进行一个小更改。xml文件,添加以下内容:

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

    mvc:annotation-driven启用上下文路径来识别spring mvc注释,以便将请求分派给控制器

    这里还有一个根上下文的完整示例。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>