Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC ViewResolver not mapping to HTML files

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>
like image 992
Mac Avatar asked Jul 27 '14 12:07

Mac


2 Answers

Check this out for mapping html files in Spring mvc (Details step is given in Answer):

Which spring view resolver plays nice with angularjs?

In simple:

In order to use static resource(html,css,img,js) in spring, use a directory structure that looks like the following:

src/
   package/
   LayoutController.java
WebContent/
   WEB-INF/
    static/
      html/
       layout.html
      images/
       image.jpg
      css/
       test.css
      js/
       main.js
     web.xml
    springmvc-servlet.xml


@Controller 
public class LayoutController {

 @RequestMapping("/staticPage") 
public String getIndexPage() { 
return "layout.htm"; 

} }




<!-- in spring config file -->
 <mvc:resources mapping="/static/**" location="/WEB-INF/static/" />

layout.html

<h1>Page with image</h1>
<img src="/static/img/image.jpg"/>
like image 170
Kuntal-G Avatar answered Nov 01 '22 13:11

Kuntal-G


This is because normally *.jsp style uri patterns are handled by the servlet container and in this specific instance *.html is not being handled by the container and instead the path is being delegated to Spring MVC which does not know how to render these extensions.

As an example, if you are using tomcat, you would see these entries under conf/web.xml file:

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

i.e jsp servlet handles *.jsp, *.jspx extension.

So given this, a potential fix will be to add .html to be added to be handled by jsp servlet, as in this link:

Using .html files as JSPs

or even better ,leave the extension as .jsp and use .html as a controller pattern instead?

like image 3
Biju Kunjummen Avatar answered Nov 01 '22 14:11

Biju Kunjummen