【问题标题】:Controller of Spring MVC does not load my HTML pageSpring MVC 的控制器不加载我的 HTML 页面
【发布时间】:2017-01-31 12:16:10
【问题描述】:

我无法理解为什么我对实现 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>

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

请注意,我同时拥有 &lt;mvc:annotation-driven/&gt;&lt;url-pattern&gt;/&lt;/url-pattern&gt;

【问题讨论】:

  • 您是否尝试访问 URL /SpringSecPaolo/
  • 由于您只是尝试提供静态内容,您可以使用 mvc:resources 配置。见docs.spring.io/spring/docs/3.2.x/spring-framework-reference/…
  • @redflar3 localhost:8080/SpringSecPaolo 准确地说
  • 你使用maven吗?您是否检查过welcome.html 确实已部署?能否请您命名welcome.html 文件的完整目录?
  • 你需要html文件还是可以用jsp替换?

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


【解决方案1】:

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

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

【讨论】:

  • 这是我正在做的,但我有一个 404,在 DispatcherServlet 中找不到带有 URI [/SpringSecPaolo/WEB-INF/views/welcome.html] 的 HTTP 请求的映射,名称为'dispatcher'
【解决方案2】:

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

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

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

  1. 使用 html 并删除 InternalResourceViewResolver
  2. 使用jsp并使用InternalResourceViewResolver

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

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

这将告诉 Spring 查看视图文件夹中的静态内容。 然后将views文件夹放在WebContent文件夹下,然后 在您的控制器 myForm() 方法中返回 html 文件,如下所示:

return "views/welcome.html";

要改用 jsp,保持 InternalResourceViewResolver 定义不变,并从

更改 suffix 属性
<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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 2014-02-12
    • 2017-08-29
    相关资源
    最近更新 更多