【问题标题】:Spring Boot MVC configuration request redirected to Dispatcher againSpring Boot MVC 配置请求再次重定向到 Dispatcher
【发布时间】: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


【解决方案1】:

我终于找到了解决办法。

包含以下依赖项可以解决问题。

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

我猜测内部存在 JSP 解析错误(或者 JSTLView 不存在),它将视图请求重定向到调度程序。

在包含这些依赖之后,JSP 视图就渲染成功了。

【讨论】:

    【解决方案2】:

    对于尝试其他解决方案但没有运气的人,您可能需要检查控制器类中是否存在 ResponseBody 注释。

    @Controller
    @EnableWebMvc
    public class MainController {
    
        @Autowired
        BO bo = new BO();
    
        @RequestMapping(value = "/sayHello",
                        method = RequestMethod.GET, 
                        produces = "application/json")
        public @ResponseBody String sayHello() {
            return "Hellaa!!";
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 2020-01-22
      • 2020-03-18
      • 2018-11-16
      • 2017-08-09
      • 2015-07-06
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多