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

Java Spring MVC的控制器不加载我的HTML页面,java,spring,spring-mvc,servlets,model-view-controller,Java,Spring,Spring Mvc,Servlets,Model View Controller,我无法理解为什么我实现SpringMVC的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

我无法理解为什么我实现SpringMVC的
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>
我有一个简单的
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>

春天|欢迎
电子邮件地址
密码
文件输入

此处为块级帮助文本示例

看看我 提交
我得到一个错误:
在名为“dispatcher”的DispatcherServlet中找不到URI为[/SpringSecPaolo/WEB-INF/views/welcome.html]的HTTP请求的映射


请注意,我有

您需要请求控制器方法映射到的url,但不能直接请求html视图


我猜您的应用程序是在以下位置部署的:
localhost:8080/SpringSecPaolo
,然后当您向以下位置发送HTTP GET时,将调用带有(
@RequestMapping(value=“/”)和
@RequestMapping(method=RequestMethod.GET)
)的控制器方法:
localhost:8080/SpringSecPaolo/
(因此只需输入此url,应用程序就会向您发送html文件)

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


我猜您的应用程序是在以下位置部署的:
localhost:8080/SpringSecPaolo
,然后当您向以下位置发送HTTP GET时,将调用带有(
@RequestMapping(value=“/”)和
@RequestMapping(method=RequestMethod.GET)
)的控制器方法:
localhost:8080/SpringSecPaolo/
(因此只需输入此url,应用程序就会向您发送html文件)

问题是您正试图使用dispatcher servlet返回html页面

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

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

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

    <mvc:resources mapping="/views/**" location="/views/" />
    
    要改为使用jsp,请保持
    InternalResourceViewResolver
    定义不变,并将后缀属性更改为

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

    
    .jsp
    
    并将welcome.html重命名为welcome.jsp

    有关html文件和Spring配置的更多信息:

    问题是您试图使用dispatcher servlet返回html页面

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

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

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

    <mvc:resources mapping="/views/**" location="/views/" />
    
    要改为使用jsp,请保持
    InternalResourceViewResolver
    定义不变,并将后缀属性更改为

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

    
    .jsp
    
    并将welcome.html重命名为welcome.jsp

    有关html文件和Spring配置的更多信息:

    您是否正在尝试访问URL
    /SpringSecPaolo/
    ?由于您只是尝试提供静态内容,您可以使用mvc:resources配置。请参阅@redflar3 localhost:8080/SpringSecPaolo以确定是否使用maven?您是否检查了welcome.html是否真的被删除了?请您命名完整的e welcome.html文件?您是否需要和html文件,或者可以用jsp替换它?您是否正在尝试访问URL
    /SpringSecPaolo/
    ?因为您只是尝试提供静态内容,所以可以使用mvc:resources配置。请参阅@redflar3 localhost:8080/SpringSecPaolo以准确说明您是否使用maven?您是否检查了welcome.html真的被删除了吗?你能说出welcome.html文件的完整目录吗?你需要一个html文件吗?或者你能用jsp替换它吗?这就是我正在做的,但是我有一个404,没有找到URI为[/SpringSecPaolo/WEB-INF/views/welcome.html]的HTTP请求的映射在名为“dispatcher”的DispatcherServlet中,我正在执行此操作,但在名为“dispatcher”的DispatcherServlet中,我有一个404,找不到URI为[/SpringSecPaolo/WEB-INF/views/welcome.html]的HTTP请求的映射
    <property name="suffix">
          <value>.jsp</value>
    </property>