我正在创建一个简单的spring mvc aplication并且我想返回.html页面,但是当我使用spring工具套件创建我的spring mvc项目时默认使用.jsp页面创建。
当我尝试访问.html页面时,它给了我这个错误
No mapping found for HTTP request with URI [/app/WEB-INF/views/test.html] in DispatcherServlet with name 'appServlet'
但是当我去.jsp页面时,它工作正常。
这是我的控制器类:
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";
}
}
这是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>
这是uri我如何尝试访问页面:
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'
答案 0 :(得分:0)
仅在控制器类中返回类似home的视图名称
return "home";
并添加servlet-context-xml
for html ==> <beans:property name="suffix" value=".html" />
for jsp ==> <beans:property name="suffix" value=".jsp" />
试试吧。
答案 1 :(得分:0)
我找不到任何可以提供纯静态HTML文件的Spring MVC ViewResolver的引用。这不是很难,但我担心你必须自己动手。
但是恕我直言,将你的HTML文件重命名为.jsp
以便Spring MVC正确处理它应该足够了。