InternalResourceViewResolver not resolving views properly

by Bill L   Last Updated November 08, 2018 23:26 PM

I have no idea what we're doing wrong here. I'm trying to set up a single page Spring MVC project so I can look into things. I have a single screen, greeting.html that I'm trying to load. Here's my controller class to load it:

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String getGreeeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);

        return "greeting";
    }

}

This works fine when greeting.html is in /resources/templates/greeeting.html. However, I'd like it behind a WEB-INF folder. I've tried placing it in WEB-INF/views/greeting.html and webapp/WEB-INF/views/greeting.html. I then try and use an InternalResourceViewResolver as I've read online:

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".html");

    return viewResolver;
}

From my understanding, this should allow it to find greeting.html behind one of my WEB-INF folder setups. However, when I go to localhost:8080/greeting, I get a 404 along with the following message in the console:

WARN 1439 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/views/greeting.html]

I'm at a complete loss as to what's even going on or how to fix it, every single tutorial online does this exact thing and has no problems, I can't seem to find anyone online with this same issue. Any guidance at all would be helpful here.



Answers 1


Hi as i understand from your question you are using spring boot By default spring boot searches for src/main/resources/templates folder If you used some dependency like :

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency>

This will enable thymeleaf by default.

To override you ViewResolver you need

In spring boot you can make such config in your application.properties file:

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.html

or you can write custom resolver configuration with java config :

@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfiguration extends WebMvcConfigurerAdapter
{
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        registry.viewResolver(resolver);
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }  
 }

I think this is helpful

Mykhailo Moskura
Mykhailo Moskura
November 08, 2018 23:13 PM

Related Questions



Spring Integration Java DSL with H2 adapter

Updated March 10, 2018 02:26 AM



convert xml config to java config for spring social

Updated February 10, 2018 11:26 AM