Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error

前端 未结 3 1325
深忆病人
深忆病人 2020-11-27 14:36

I\'m trying to make \"hello world\" application with gradle, spring boot and spring mvc with the simplest view resolver and html.

I started from the thymeleaf spring

相关标签:
3条回答
  • 2020-11-27 14:45

    View resolver can also be configured in application.properties file of Spring-Boot web applications, something like below:

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    
    0 讨论(0)
  • 2020-11-27 14:57

    After investigating more I discovered an alternative solution that works without adding configureDefaultServletHandling method. You need to add an embedded tomcat jsp engine to build.gradle:

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

    As opposed to configureDefaultServletHandling method this solution works not only with plain html but also with jsp.

    All solutions are available at: https://github.com/driver-pete/spring-mvc-example This solution is available on master. Biju's solution is on DefaultServletHandling_solution branch.

    0 讨论(0)
  • 2020-11-27 15:00

    You only need to enable the default servlet, this is done by adding the following to your 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();
        }    
    }
    

    Essentially what is happening is Spring does not know how to handle the handling of such content natively(could be a jsp say), and to this configuration is the way to tell it to delegate it to the container.

    0 讨论(0)
提交回复
热议问题