Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InternalResourceViewResolver not resolving views properly

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.

like image 988
Bill L Avatar asked Nov 08 '18 20:11

Bill L


4 Answers

Try removing provided or add ${tomcat-embed-jasper.scope} in scope tag from tomcat-embed-jasper dependency . It helped for me.

like image 139
Yadnit Avatar answered Oct 20 '22 01:10

Yadnit


Try changing your view resolver extending WebMvcConfigurerAdapter.

@Configuration
@ComponentScan(basePackages="...package name...")
@EnableWebMvc

public class MvcConfiguration extends WebMvcConfigurerAdapter{

  @Bean
  public InternalResourceViewResolver viewResolver() {

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

   return viewResolver;
}

}

like image 37
Himanshu Avatar answered Oct 20 '22 03:10

Himanshu


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

like image 32
Mykhailo Moskura Avatar answered Oct 20 '22 03:10

Mykhailo Moskura


I tried all the solutions but got the 404 error using Spring Boot 2.*. For me, running the application using the command "mvn clean spring-boot:run" fixed the error

like image 25
Pavan Ulichi Avatar answered Oct 20 '22 01:10

Pavan Ulichi