2014-12-22 83 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

相關問題