【发布时间】:2016-10-15 15:45:16
【问题描述】:
我正在努力解决此处描述的问题
No mapping found for HTTP request with URI Spring MVC
问题是一旦资源解析器解析了视图,它又被重定向到 Dispatcher,它找不到映射到它的 a 方法。
例如,如果用户请求 /test 并且解析器将其映射到 /views/test.jsp,则调度程序不会呈现响应,而是尝试再次查找路径 /views/test.jsp。
我已尝试将默认调度程序 servlet 路径更改为 /(顺便说一下,默认情况下为 /)。
它不适用于 Spring Boot 配置。
我想知道是否有解决方案,而无需将调度程序路径设置为 / 以外的其他内容,例如 /page。
以下是产生问题的测试代码。 (这里是测试项目的github链接https://github.com/ConsciousObserver/stackoverflow/tree/master/TestMvc)
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
public class TestMvcApplication {
public static void main(String[] args) {
SpringApplication.run(TestMvcApplication.class, args);
}
}
@Configuration
class MvcConfig extends WebMvcConfigurerAdapter {
public MvcConfig () {
System.out.println("%%%%%%%%%%%%% " + getClass() + " loaded %%%%%%%%%%%%%%%");
}
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet);
System.out.println("%%%%%%%%%%%%%%%%% Adding dispatcher servletmapping");
registration.addUrlMappings("/");
return registration;
}
}
@Controller
class TestController {
@RequestMapping("test")
public String test() {
return "test";
}
}
application.properties
logging.level.org.springframework.web=DEBUG
spring.mvc.view.prefix: /views/
spring.mvc.view.suffix: .jsp
以下是相关日志
Returning [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/views/test.jsp]] based on requested media type 'text/html'
Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'test'; URL [/views/test.jsp]] in DispatcherServlet with name 'dispatcherServlet'
Forwarding to resource [/views/test.jsp] in InternalResourceView 'test'
DispatcherServlet with name 'dispatcherServlet' processing GET request for [/views/test.jsp]
Looking up handler method for path /views/test.jsp
Did not find handler method for [/views/test.jsp]
Matching patterns for request [/views/test.jsp] are [/**]
URI Template variables for request [/views/test.jsp] are {}
Mapping [/views/test.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@21362712]]] and 1 interceptor
Last-Modified value for [/views/test.jsp] is: -1
Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
Successfully completed request
Successfully completed request
【问题讨论】:
-
删除你的
MvcConfig,因为这会破坏正确的集成,你真的想使用JSP吗?这仅适用于war打包,并且在您的情况下,jsp 文件需要进入webapp/views。 -
你能否详细说明它是如何破坏配置的,删除它没有帮助?是的,我需要 JSP,因为我正在将遗留项目转换为 Spring Boot,它有很多 JSP,我还没有准备好迁移到任何其他视图层。
-
这很糟糕,因为它禁用了(部分)您可能仍然希望工作的自动配置。
-
在 Spring Boot 中覆盖 MVC 配置的正确方法是什么?
-
您需要覆盖哪些尚未配置的内容?
标签: spring spring-mvc spring-boot