2016-09-22 77 views
0

我無法理解爲什麼我的簡單配置DispatcherServlet來實現Spring MVC不起作用。Spring MVC的控制器不會加載我的HTML頁面

web.xml看起來像如下:

<?xml version="1.0" encoding="UTF-8"?> 

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

</web-app> 

我的分發程序Servlet的應用程序上下文是dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

    <context:component-scan base-package="org.paolo.controller" /> 
    <mvc:annotation-driven/> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.html</value> 
     </property> 
    </bean> 

</beans> 

我有一個簡單MyController處理請求

package org.paolo.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping(value="/") 
public class MyController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String myForm() { 
     return "welcome"; 
    } 
} 

並根據WEB-INF/views我把我的welcome.html頁,這是一個簡單的引導形式:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="ISO-8859-1"> 
<title>Spring | Welcome</title> 

<!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 

<!-- Optional theme --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> 

<!-- Latest compiled and minified JavaScript --> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 

</head> 
<body> 

<form> 
    <div class="form-group"> 
    <label for="exampleInputEmail1">Email address</label> 
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email"> 
    </div> 
    <div class="form-group"> 
    <label for="exampleInputPassword1">Password</label> 
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password"> 
    </div> 
    <div class="form-group"> 
    <label for="exampleInputFile">File input</label> 
    <input type="file" id="exampleInputFile"> 
    <p class="help-block">Example block-level help text here.</p> 
    </div> 
    <div class="checkbox"> 
    <label> 
     <input type="checkbox"> Check me out 
    </label> 
    </div> 
    <button type="submit" class="btn btn-default">Submit</button> 
</form> 

</body> 
</html> 

我得到一個錯誤:
No mapping found for HTTP request with URI [/SpringSecPaolo/WEB-INF/views/welcome.html] in DispatcherServlet with name 'dispatcher'

注意,我都<mvc:annotation-driven/><url-pattern>/</url-pattern>

+0

你試圖訪問URL'/ SpringSecPaolo /'? –

+1

由於您只是想要提供靜態內容,因此可以使用mvc:resources配置。請參閱http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-static-resources –

+0

@ redflar3 localhost:8080/SpringSecPaolo準確地說是 – DistribuzioneGaussiana

回答

1

您需要請求控制器方法映射到的url,而不是直接的html視圖!

我猜你的申請是根據部署:當你發送一個HTTP GET到localhost:8080/SpringSecPaolo然後用(@RequestMapping(value="/")@RequestMapping(method = RequestMethod.GET))控制器方法被調用:localhost:8080/SpringSecPaolo/(僅輸入這個網址,應用程序應該送你的HTML文件)

+0

這就是我正在做的事情,但我有一個404,沒有找到映射與DispatcherServlet中的名爲'dispatcher'的HTTP請求與URI [/SpringSecPaolo/WEB-INF/views/welcome.html] – DistribuzioneGaussiana

1

問題是你試圖用調度器servlet返回一個html頁面。

html文件是靜態的,它們不需要由servlet處理。

解決你的問題,你有兩個選擇:

  1. 使用html和刪除InternalResourceViewResolver
  2. 使用JSP和使用InternalResourceViewResolver

使用HTML,你必須刪除InternalResourceViewResolver定義(或者設置爲空白前綴和後綴屬性)並將此行添加到dispatcher-servlet.xml

<mvc:resources mapping="/views/**" location="/views/" /> 

這將告訴Spring查看靜態內容的視圖文件夾。 然後把意見文件夾中的的WebContent文件夾下,並 在您的控制器myForm會()方法返回的HTML文件中像這樣:

return "views/welcome.html"; 

JSP中使用,而不是保持InternalResourceViewResolver定義是與變化從

<property name="suffix"> 
     <value>.html</value> 
</property> 

後綴屬性

<property name="suffix"> 
     <value>.jsp</value> 
</property> 

,顯然命名welcome.html來的welcome.jsp

有關HTML文件和Spring配置的詳細信息: How to serve .html files with Spring

相關問題