0

I was trying to add some HTML files to the spring project that I was working on. Initially, the project was working fine with JSP files.

This is the folder structure that I'm following: /WEB-INF/views/jsp/hello.jsp

Spring web configuration is as follows:

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/jsp");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

I tried to replace it with HTML as follows:

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/static/html/");
    viewResolver.setSuffix(".html");
    return viewResolver;
}

And the request mapping is as follows:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String myMethod(ModelMap model) {
    return "index";
}

Everything is working fine as long as the page is a JSP file. When changed to HTML, it'll start giving errors.

This is the log entry:

15-Dec-2016 11:54:57.408 WARNING [http-apr-9999-exec-2] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/WEB-INF/views/html/index.html] in DispatcherServlet with name 'dispatcher'

Martin Nyolt
  • 4,463
  • 3
  • 28
  • 36
Anand MP
  • 121
  • 1
  • 2
  • 13
  • Please read this [post](http://stackoverflow.com/questions/16598594/how-to-map-requests-to-html-file-in-spring-mvc). Maybe is helpful for you –  Dec 15 '16 at 07:01
  • The code you are showing is launching hello.html, but in log you are displaying index.html. Do you have index.html file in your folder? – Rajashekhar Dec 15 '16 at 07:05
  • @Rajashekhar I added the working piece of code by mistake. It's actually index.html. I've edited it in the question. Thanks for pointing it out. – Anand MP Dec 15 '16 at 07:10
  • Have you change index.jsp to index.html? – Sanjay Dec 15 '16 at 08:24
  • @SanjayPatel Yes, I did – Anand MP Dec 15 '16 at 09:38
  • Possible duplicate of [How to serve .html files with Spring](http://stackoverflow.com/questions/15479213/how-to-serve-html-files-with-spring) – Bacteria Dec 15 '16 at 14:45

1 Answers1

0

maybe you need the following code in file dispatcher-servlet.xml

<!-- JSP VIEW RESOLVER -->
<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
Úrsula
  • 21
  • 4