Fausto
Fausto

Reputation: 181

spring mvc can't access .html files

I searched all over the site and nothing suggested worked! I'm tryng to make a spring-mvc and angularJS application; This is my project structure:

angularState
-src/main/java/it.controller.DefaultController.java
-src/main/webapp
--index.jsp
--WEB-INF
---dispatcher-servlet.xml
---web.xml
---static
----default.html

so in my web.xml I have:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/dispatcher-servlet.xml
    </param-value>
</context-param>

in the dispatcher-servlet.xml:

<context:component-scan base-package="it.controller"></context:component-scan>
<mvc:annotation-driven />
<context:annotation-config />
<mvc:resources location="/WEB-INF/static/" mapping="/static/**" />

in the index.jsp:

<% response.sendRedirect("default"); %>

then in the controller:

@Controller
@RequestMapping("/default")
public class DefaultController {
public String showDefault(){
    return "/static/default.html";
}
}

I've always used jsp and everything was fine; now I've tried every single question of the site: I've tried -mvc:default-servlet-handler -InternalResourceViewResolver with prefix "/WEB-INF/static" and suffix ".html" or even suffix void -InternalResourceViewResolver void and the controller method returning "/WEB-INF/static/default.html" NOTHING! nothing showed the default.html. why that .html file is so complicated!

Upvotes: 0

Views: 2339

Answers (2)

Fausto
Fausto

Reputation: 181

For anyone who will google, here the answer. the problem was pretty simple: All I was wrong was the controller method; the original controller method was:

@Controller
@RequestMapping("/default")
public class DefaultController {
    public String showDefault(){
        return "/static/default.html";
    }
}

the correct version must be:

@Controller
@RequestMapping("/default")
public class DefaultController {
@RequestMapping(method=RequestMethod.GET)
    public String showDefault(){
        return "/static/default.html";
    }
}

because every page must have a GET access, more than all, the homepage! Because of that missing @RequestMapping on the method, every config I tried gave me errors on the .html files. As I sayd, .html files only needs the

<mvc:resources location="/WEB-INF/static/" mapping="/static/**" />

to be configured because they're static resources and they don't need the help of the servlet. Hope this will help someone, thanks to everyone who tried.

Upvotes: 0

Ankur Singh
Ankur Singh

Reputation: 335

This is because normally *.jsp style uri patterns are handled by the servlet container and in your case *.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 a quick fix you can try below code in conf/web.xml

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

Or

Content negotiating view resolver can also be used in place of InternalResourceViewResolver(I Guess you might be using this) which return a suitable view based on file extension declared in “mediaTypes” property) Example

 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
          <property name="order" value="1" />
          <property name="mediaTypes">
            <map>
               <entry key="json" value="application/json" />
                <entry key="html" value="text/html"/>
            </map>
          </property>

Upvotes: 1

Related Questions