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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for response, yous solution would be good, but I want to use html, then I can use bower to import web dependencies. Already fixed it, no idea why but it was because spring version.
Yea, no problem. glad that you solved this. my solution 2 should work with html.
ConfigureViewResolvers fixed this issue for me. Cheers!
adding Thymeleaf dependency resolved my problem. Tnx.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.