Skip to content
Advertisement

HTTP Status 404 – Not Found: for controller – no view resolvers found in Idea

I have the following Spring MVC code:

config files:

public class MainWebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(final ServletContext sc) throws ServletException {

        AnnotationConfigWebApplicationContext root =
                new AnnotationConfigWebApplicationContext();

        root.register(WebConfig.class);
        root.setServletContext(sc);

        root.scan("testspring");
        sc.addListener(new ContextLoaderListener(root));

        ServletRegistration.Dynamic appServlet =
                sc.addServlet("dispatcher", new DispatcherServlet(new GenericWebApplicationContext()));
        appServlet.setLoadOnStartup(1);
        appServlet.addMapping("/");
    }
}
@EnableWebMvc
@Configuration
@ComponentScan("testspring")
public class WebConfig extends WebMvcConfigurerAdapter {
/*
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }*/

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();

        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/view/");
        bean.setSuffix(".html");

        return bean;
    }
}

contollers:

@Controller
public class HomeController {
    @RequestMapping(value = "/")
    public String home() {
        System.out.println("HHomeController: Passing through...");
        return "home.html";
    }
}
@RestController
public class TestRestController {
    @RequestMapping(value = "/api")
    public String home() {
        System.out.println("Rest: Passing through...");
        return "some json";
    }
}

I get the following errors when I deploy:

enter image description here

enter image description here

Accessing the /api page, however, works just fine.

EDIT:

After replacing home.html with home, the error is still present – both in IDE and the 404 in browser:

enter image description here

Also maybe it’s relevant – I get this pop up at one of the config classes:

enter image description here

Advertisement

Answer

The problem was me using JstlView.class and ViewResolver with .html pages that I wanted to render via thymeleaf. If I change the code inside WebConfig to:

   @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();

        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/view/");
        bean.setSuffix(".jsp");

        return bean;
    }

and replace home.html with home.jsp it works just fine.

Not sure how I’d need to do with thymeleaf. I decided to go with rest controllers instead, but maybe someone would be able to post thymeleaf-friendly version of the code later. I was recommended this: https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html for writting a correct method. Might come back to this answer to edit later.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement