gpt4 book ai didi

java - HTTP 请求未委托(delegate)给根 WebApplicationContext 的 Controller

转载 作者:行者123 更新时间:2023-11-29 08:33:17 25 4
gpt4 key购买 nike

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

但问题是 HTTP 请求没有被委托(delegate)给根 WebApplicationContext 的 Controller 。

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

服务器日志说:

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.

最佳答案

只需对 root-context.xml 文件进行一点小改动,添加:

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

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

这里还有一个 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>

关于java - HTTP 请求未委托(delegate)给根 WebApplicationContext 的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46107104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com