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 :
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.
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.
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.
Pult all your htmlviews folder under resources so that Spring can find it according to your Resource Resolver.
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.