I cannot get spring mvc to resolve .html view files.
I have the following view folder structure:
WEB-INF
`-views
|- home.jsp
`- home.html
I have a simple hello world controller method that just prints a message and returns the view name "home". I have a home.jsp file, but would like to use the home.html instead.
<!-- Working servlet mapping -->
<servlet-mapping>
<servlet-name>spaceShips</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- working servlet context -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
When I hit spaceships/home the controller prints the hello world message and I see the home.jsp view without a problem.
The problem is when I change the suffix to .html.
After changing the suffix and navigating to /home, the controller prints the message however I see a 404 error in the browser and the following in the console: WARNING: No mapping found for HTTP request with URI [/spaceships/WEB-INF/views/home.html]
To clarify:
<!-- not working with .html -->
<servlet-mapping>
<servlet-name>spaceShips</servlet-name>
<!-- I have tried /* here as well without success -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- not working with .html-->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="WEB-INF/views/" />
<beans:property name="suffix" value=".html" />
</beans:bean>
I have checked in the exploded war folder and can confirm that both home files are present.
Has anyone encountered something like this before?
Last chunk of console message:
INFO: Server startup in 5256 ms
Hello, World!
Jul 27, 2014 12:52:01 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spaceships/WEB-INF/views/home.html] in DispatcherServlet with name 'spaceShips'
Thanks for reading.
=========== SOLUTION ============
The following (ugly) configuration solved the issue. There are probably ways to clean this up, but if you are experiencing the same problem you may be able to piece together a solution from this.
Folder structure:
WEB-INF
`-static
|-html
`-home.html
|-css
`-img
Controller method:
@RequestMapping(value = "/home")
public String goHome() {
System.out.println("lolololololol");
return "static/html/home";
}
Spring config:
<resources mapping="/static/**" location="/WEB-INF/static/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="" />
<beans:property name="suffix" value=".html" />
</beans:bean>
<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency>
– Ali mohammadi