Spring MVC的控制器不加载我的HTML页面

时间:2016-09-22 19:13:30

标签: java spring spring-mvc servlets model-view-controller

我无法理解为什么我实现Spring MVC的DispatcherServlet的简单配置不起作用。

我的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>

2 个答案:

答案 0 :(得分:1)

您需要请求控制器方法映射到的URL,而不是直接请求html视图!

我猜您的应用程序部署在:localhost:8080/SpringSecPaolo下,然后当您将HTTP GET发送到@RequestMapping(value="/")时调用带有(@RequestMapping(method = RequestMethod.GET)localhost:8080/SpringSecPaolo/)的控制器方法(所以只需输入此URL,应用程序应该发送html文件)

答案 1 :(得分:1)

问题是你正在尝试使用调度程序servlet返回一个html页面。

html文件是静态的,它们不需要由servlet处理。

要解决您的问题,您有两种选择:

  1. 使用html并移除InternalResourceViewResolver
  2. 使用jsp并使用InternalResourceViewResolver
  3. 要使用html,您必须删除InternalResourceViewResolver定义(或设置为空白前缀和后缀属性)并将此行添加到dispatcher-servlet.xml

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

    这将告诉Spring查看静态内容的views文件夹。 然后将views文件夹放在 WebContent 文件夹下,并且 在你的Controller 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