2

I'm just starting with Spring MVC. Created a default template with IntelliJ, and now I'm trying to add some content.

The default extension for the pages is .jsp, but I want to change my pages to .html extension and can't figure out how.

So I have about.jsp file and I want to change it to about.html. I changed the filename itself and in the file mvc-dispatcher-servlet.xmlI changed:

<property name="suffix" value=".jsp"/>

To:

<property name="suffix" value=".html"/>

After my change I get:

05-Jul-2016 15:31:25.788 WARNING [http-nio-8082-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/WEB-INF/view/pages/about.html] in DispatcherServlet with name 'mvc-dispatcher'

And a page with 404 status.

This my directory tree:

Working dir

mvc-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.weitz.alex"/>

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

web.xml:

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>

</servlet-mapping>

HelloController.java:

package com.weitz.alex;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "about";
    }
}
1

2 Answers 2

1

remove this line <property name="suffix" value=".html"/> since html pages are static handle it as a resource add these lines to the configuration.

<mvc:resources mapping="/pages/**" location="/WEB-INF/view/pages/" />
<mvc:annotation-driven/>

there is no need to handle this with a viewresolver.

and change your return statement in the controller to this.

return "redirect:/pages/about.htm";

Sign up to request clarification or add additional context in comments.

Comments

0

It looks to me like you're missing a resource mapping for your .html pages. Try adding one to your mvc-dispatcher-servlet.xml. An example could look like this:

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

For a more detailed description of your problem, you might also have a look at this answer, which shows the solution for a very similar problem.


I'd also recommend you to change your folder structure and place your files in a folder called "/static/resources/" or similar because your .html files are static (your jsp files weren't).

Your prefix and suffix properties look like they should just work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.