8

I get such exception:

javax.servlet.ServletException: Could not resolve view with name 'htmlviews/index.html' in servlet with name 'dispatcher'
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1211)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

when I try to connect to fully java configured spring web service.

My configuration classes:

@Configuration
@EnableWebMvc
@ComponentScan({"config", "controller"})
public class MyWebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/htmlviews/**").addResourceLocations("/htmlviews/");
    }
}

Initializer:

    public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{MyWebConfig.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

and controller:

@Controller
public class IndexController {

    @RequestMapping(value = "/")
    public String getIndexPage() {
        return "htmlviews/index.html";
    }

    @RequestMapping(value = "/{[path:[^\\.]*}")
    public String index() {
        return "forward:/";
    }
}

whole file srtucture is simple :

whole file srtucture is simple :

I am using Idea IDE (also tried in eclipse, same exception) and trying to deploy on tomcat. In pom.xml, I added 'jstl' dependency, but that did not help to resolve problem. Using xml configuration everything works well. I have no idea what is wrong with my spring java configuration, it is super simple, maybe I forgot something?

Fixed it Everything started working when I changed spring version from 4.1.0.RELEASE to 4.2.3.RELEASE . I do not why it does not work with 4.1.0.RELEASE. Maybe someone can explain, just curious.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Edgar
  • 1,120
  • 4
  • 28
  • 53

2 Answers2

14

Problem

Spring is trying to find views under your webapp directory. Since you do not have any view resolver, Spring cannot resolve "htmlviews/index.html". In other words, Spring does not know what it is.
You have a Resource Resolver for your html page, which is OK because HTML is static.

Possible Solution 1

In your MyWebConfig class, add the following:

@Override
public void configureViewResolvers(final ViewResolverRegistry registry) {
    registry.jsp("/htmlviews/", ".jsp");
}  

OR you can do this:

@Bean
public InternalResourceViewResolver jspViewResolver() {
    InternalResourceViewResolver resolver= new InternalResourceViewResolver();
    resolver.setPrefix("/htmlviews/");
    resolver.setSuffix(".jsp");
    return resolver;
}  

Change your html page to jsp page, I recommend that because jsp is simply more powerful than HTML.

Possible Solution 2

Pult all your htmlviews folder under resources so that Spring can find it according to your Resource Resolver.

Update

It's rarely the case that HTML is needed in a Spring boot app. I highly recommend using a template engine (Thymeleaf is preferred). This way, the sensible default setup is sufficient for most of the multi-page applications.

Minjun Yu
  • 3,497
  • 4
  • 24
  • 39
0

I was trying to implement demo https://spring.io/guides/gs/securing-web/ but i was facing similar problem, To note- this demo only have html with thymleaf (no JSP) and I missed to add thymleaf dependency(reason of error) earlier it showed error

Circular view path []: would dispatch back to the current handler URL ..error

Then I added bean view resolver and it started to give error .

Could not resolve view with name..error

Finally it worked after removing the bean view resolver and adding dependency for thymleaf. Adding this made my project work.

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

Some more help I found to understand all this working at How to avoid the "Circular view path" exception with Spring MVC test

Vikash
  • 2,046
  • 3
  • 23
  • 30