2014-12-22 85 views
1
页面

,我想返回的.html页面,但是当我创建一个使用弹簧工具我的春天MVC项目套件默认情况下被使用创建.jsp页面。的DispatcherServlet不返回html的网页,但它可以返回的.jsp我创建一个简单的Spring MVC aplication

当我尝试去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上下文的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

检查此链接:http://stackoverflow.com/questions/20564336/internalresourceviewresolver-to-resolve-both-jsp-and-html-together –

+0

我有我的问题相同的代码,但仍然没有工作,但在该人说它的作品的链接,为什么这项工作对某些人和其他人不?我不发表我的根XML怎么一回事,因为我认为这不什么都没有做与此错误 – stackUser2000

回答

0

回报只有在控制器类视图名称,如家

return "home"; 

,并添加servlet的上下文XML

尝试它。

+0

因为我有两个的.jsp和.html页面我不得不返回becasue我有我的空白<豆后缀类型:属性名= “后缀”值=“” /> – stackUser2000

+0

它给了我这个errror财产'多“属性”定义“suffix'' – stackUser2000

0

我无法找到一个Spring MVC的ViewResolver的,将成为普通的静态HTML文件的任何引用。这不是很难,但恐怕你不得不推出自己的。

但恕我直言,这应该足够你的HTML文件重命名为.jsp有它正确地Spring MVC的处理。

+0

这是奇怪的东阳我已经做到了这一点,但使用Java类的配置,而不是一个xml配置,但我不能找到那个例子 – stackUser2000

相关问题