【问题标题】:Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error使用 Spring Boot 和注释配置 ViewResolver 给出 No mapping found for HTTP request with URI error
【发布时间】:2023-03-29 21:40:02
【问题描述】:

我正在尝试使用 gradle、spring boot 和 spring mvc 以及最简单的视图解析器和 html 来制作“hello world”应用程序。

我从 thymeleaf spring boot example 开始,我只是想删除 thymeleaf 以使用纯 html 和 InternalResourceViewResolver 制作更简单的 mvc 应用程序。我有一个要服务的 greeting.html,它位于 src/main/webapp/WEB-INF。当我运行应用程序时,我得到了

No mapping found for HTTP request with URI [/WEB-INF/greeting.html] in DispatcherServlet with name 'dispatcherServlet'

这是一个常见错误,网上有很多答案,但似乎没有任何帮助。

这是我的 Application.java

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

这是我的 GreetingController.java

@Controller
public class GreetingController {
    @RequestMapping("/greeting")
    public String greeting() {
        return "greeting";
    }
}

这是我的 MvcConfiguration.java

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".html");
        return resolver;
    }
}

我用gradle bootRun 运行它

这里是带有代码的仓库:https://github.com/driver-pete/spring-mvc-example

这里还有一些线索:

  • Thymeleaf 视图解析工作正常
  • InternalResourceViewResolver 解析到正确的路径
  • WEB-INF 和 greeting.html 似乎存在于 war 文件中
  • 我没有 jsp 或 jstl,所以我不会像某些人建议的那样错过那些 jars

我的假设是调度程序 servlet 以某种方式被配置为在 /* 而不是 / 上服务,例如 here 和任何地方。但是我没有 web.xml 所以这些建议在这里不适用。我看到很多示例如何以编程方式配置调度程序 servlet,但我想将我的应用程序保持在最低限度,我怀疑 spring boot 应该可以配置它,因为它与 thymeleaf 配合得很好。

【问题讨论】:

  • 设置视图解析器前缀和后缀可以通过将这些属性放在 application.properties 文件中来简化:spring.mvc.view.prefix=/WEB-INF/spring.mvc.view.suffix=.html

标签: java spring spring-mvc gradle spring-boot


【解决方案1】:

如果你使用 5.0 以上的 spring,那么使用org.springframework.web.servlet.view.InternalResourceViewResolver 代替 org.springframework.web.servlet.InternalResourceViewResolver 在你的 bean 定义中

【讨论】:

    【解决方案2】:

    视图解析器也可以在 Spring-Boot Web 应用程序的application.properties 文件中配置,如下所示:

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    

    【讨论】:

    • 将视图解析器配置为jsp的简单而干净的方法
    【解决方案3】:

    在进行了更多调查后,我发现了一种无需添加 configureDefaultServletHandling 方法即可工作的替代解决方案。需要在 build.gradle 中添加嵌入式 tomcat jsp 引擎:

    compile("org.apache.tomcat.embed:tomcat-embed-jasper")
    

    与 configureDefaultServletHandling 方法相反,此解决方案不仅适用于纯 html,还适用于 jsp。

    所有解决方案都可以在:https://github.com/driver-pete/spring-mvc-example 此解决方案在 master 上可用。 Biju 的解决方案在 DefaultServletHandling_solution 分支上。

    【讨论】:

    • 非常感谢 :) 我有一个基于旧 spring mvc 框架的源代码,我想将它迁移到基于 spring boot 的新 spring mvc 以便更好、更轻松地开发和部署。你为我节省了很多时间
    【解决方案4】:

    您只需要启用默认 servlet,只需将以下内容添加到您的 MvcConfiguration

    @Configuration
    @EnableWebMvc
    public class MvcConfiguration extends WebMvcConfigurerAdapter{
        @Bean
        public ViewResolver getViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/");
            resolver.setSuffix(".html");
            return resolver;
        }
    
        @Override
        public void configureDefaultServletHandling(
                DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }    
    }
    

    基本上发生的事情是 Spring 不知道如何本地处理此类内容的处理(可能是一个 jsp 说),并且这种配置是告诉它将其委托给容器的方式。

    【讨论】:

    • 谢谢!这确实解决了错误。然而不知何故,文档docs.spring.io/spring/docs/current/spring-framework-reference/… 以某种方式给人一种印象,即 spring 可以开箱即用地处理 html 和 jsps。看起来回退到默认 servlet 被描述为一种可能性,但不是提供 jsps 的正常方式。我错过了什么?
    • 另外,与 web.xml 类似的错误通常在更改 DispatcherServlet 映射后自行解决。为什么注解配置不同?我错过了什么?
    • 与添加“tomcat-embed-jasper”相比,它为我解决了问题,您的解决方案导致 .jsp 显示为“原始”:它是 jsp 文件的原始输出,包括所有标签并且不呈现为 html。
    • WebMvcConfigurerAdapter 自 Spring 5 起已弃用;使用WebMvcConfigurer,如下所述:stackoverflow.com/questions/47552835/…
    • WebMvcConfigurerAdapter 已被弃用,您可能需要实现 WebMvcConfigurer
    猜你喜欢
    • 2015-12-06
    • 2017-05-14
    • 1970-01-01
    • 2015-04-06
    • 2012-01-24
    • 1970-01-01
    • 2015-03-23
    相关资源
    最近更新 更多