Marko
Marko

Reputation: 128

Spring not redirecting to html page

Here is my view resolver:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".html"/>
</bean>

I also have a controller that should return hello.html:

@Controller
@RequestMapping("/")
public class IndexController {

    @RequestMapping(method = RequestMethod.GET)
    public String getHtmlPage() {
        return "hello";
    }
}

When I access localhost:8080 I get an error message:

WARNING: No mapping found for HTTP request with URI [/WEB-INF/pages/hello.html] in DispatcherServlet with name 'mvc-dispatcher'

Now when I change my suffix value to .jsp, then hello.jsp is returned correctly.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

hello.jsp and hello.html are in the same folder. Any ideas how to fix this?

EDIT:

For html you don't need view resolvers. Instead just create a folder in your webapp folder. Call it static for example , then add <mvc:resources mapping="/static/**" location="/static/" /> to your xml.

In controller instead of return "hello"; put return "static/hello.html";

Upvotes: 2

Views: 1721

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148910

As a complement to answers of How to map requests to HTML file in Spring MVC?, I would say that the InternalResourceViewResolver knows how to render JSP or JSP+JSTL views. You could of course use HTML views - even if it makes little sense(*) - by writing a custom ViewResolver that would be able to render plain HTML pages.

(*) Normally, the controller prepares a model that will be used by the view. In a plain HTML view, you cannot use the model. Normally when you try to do that, you actually needs to redirect to a plain HTML page, not use it like a view.

To redirect to the HTML page, you put it out of the WEB-INF folder, in a place accessible as a resource.

But in your particular need, you are trying to display hello.html for the root URL. The best way to do it is to put it directly at the root of the web application folder atn declare it in the welcome-file-list of the web.xml :

<welcome-file-list>
    <welcome-file>hello.html</welcome-file>
</welcome-file-list>

Upvotes: 1

Related Questions