Controller of Spring MVC does not load my HTML pag

2019-09-08 15:11发布

问题:

I'm not able to understand why my simple configuration of DispatcherServlet to implement Spring MVC does not work.

My web.xml looks like as follow:

<?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>

The application context of my dispatcher servlet is in 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>

I have a simple MyController to handle the request

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";
    }   
}

And under WEB-INF/views I put my welcome.html page, that is a simple bootstrap form:

<!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>

I get an error:
No mapping found for HTTP request with URI [/SpringSecPaolo/WEB-INF/views/welcome.html] in DispatcherServlet with name 'dispatcher'

Note that I have both <mvc:annotation-driven/> and <url-pattern>/</url-pattern>

回答1:

You need to request the url to which the controller method is mapped, but not the html view directly!

I guess your application is deployed under: localhost:8080/SpringSecPaolo then your controller method with (@RequestMapping(value="/") and @RequestMapping(method = RequestMethod.GET)) gets invoked when you send an HTTP GET to: localhost:8080/SpringSecPaolo/ (so just enter this url, and the application should send you the html file)



回答2:

The problem is you are trying to return a html page with the dispatcher servlet.

html files are static and they do not require to be processed by a servlet.

To solve your problem you have two choices:

  1. Use html and remove the InternalResourceViewResolver
  2. Use jsp and use the InternalResourceViewResolver

To use html you have to remove the InternalResourceViewResolver definition (or set to blank the prefix and suffix attributes) and add this line to the dispatcher-servlet.xml

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

This will tell Spring to look into views folder for static content. Then put the views folder under the WebContent folder, and in your Controller myForm() method return the html file like this:

return "views/welcome.html";

To use jsp instead keep the InternalResourceViewResolver definition as is and change the suffix property from

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

to

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

and obviously rename welcome.html to welcome.jsp

For more information about html files and Spring configuration: How to serve .html files with Spring