1

I'm creating a simple spring mvc aplication and i want to return .html pages but when i create my spring mvc project using spring tools suite by default gets created with .jsp pages.

When i try to go to a .html page it gives me this error

No mapping found for HTTP request with URI [/app/WEB-INF/views/test.html] in DispatcherServlet with name 'appServlet'

but when i go to a .jsp page it works fine.

This is my controller class:

package com.abc.app;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        return "home.jsp";
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(Model model) {
        logger.info("prueba html");



        return "test.html";
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public String test2(Model model) {
        logger.info("prueba html");



        return "test2.jsp";
    }

}

This is mye servlet-context-xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value="" />
    </beans:bean>

    <context:component-scan base-package="com.abc.app" />



</beans:beans>

This is the uri how i try to acces the pages:

http://localhost:8080/app/test2    <--- this is the .jsp page and it works

http://localhost:8080/app/test    <--- this is the .html page and it dont work it gives me this error  WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/app/WEB-INF/views/test.html] in DispatcherServlet with name 'appServlet'
stackUser2000
  • 1,615
  • 11
  • 32
  • 55
  • Check this link: http://stackoverflow.com/questions/20564336/internalresourceviewresolver-to-resolve-both-jsp-and-html-together – Sushant Tambare Dec 22 '14 at 15:51
  • i have the same code in my question but still dont work, but in the link the person says that it works, why this work for some people and other not?? i dont post my root-xml beacuse i think that it dont have nothing to do with this error – stackUser2000 Dec 22 '14 at 16:03

2 Answers2

0

return only view name like home in controller class

return "home";

and add in servlet-context-xml

for html ==>  <beans:property name="suffix" value=".html" />
for jsp ==>  <beans:property name="suffix" value=".jsp" />

Try its.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

I could not find any reference for a Spring MVC ViewResolver that would serve plain static HTML files. It is not very hard, but I'm afraid you will have to roll your own.

But IMHO, it should be enough to rename your HTML file to .jsp to have it correctly processed by Spring MVC.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • that's wierd beacause i have done this but with a java class configuration instead of a xml configuration, but i cant find that example – stackUser2000 Dec 22 '14 at 15:48