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'