百里香叶不能用Spring MVC渲染

EI-*_*-01 0 java html5 spring spring-mvc

您好,我是春天的新手,我正在尝试使用百里香叶显示一个简单的html文件,但呈现视图时遇到问题。它仅显示字符串“ index”,而不显示index.html中的html内容。请有人能告诉我我做错了什么

web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Hotel Management System</display-name>
    <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>
Run Code Online (Sandbox Code Playgroud)

dispatcher-servle.xml文件:

<context:component-scan base-package="org.myExample.controller" />

       <bean class ="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
       <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
       <bean name="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
           <property name="prefix" value="/WEB-INF/templates"/>
           <property name="suffix" value=".html"/>
           <property name="templateMode" value="HTML5"/>
        </bean>

        <bean name="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
            <property name="templateResolver" ref="templateResolver"/>
        </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
    </bean>
       <mvc:annotation-driven />
</beans>
Run Code Online (Sandbox Code Playgroud)

TestController.java:

@RestController
public class TestController {


    @RequestMapping(value = "/")
    public String printWelcome() {

        return "index";
    }
}
Run Code Online (Sandbox Code Playgroud)

index.html文件:在webapp / templates / index.html中定义

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initialscale=1">
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5
   elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the
   page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/
3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/
1.4.2/respond.min.js"></script>
    <![endif]-->

    <style>

        .box {

            background-color:#d3d3d3

        }

    </style>
</head>
<body>
<h1>Hello, world!</h1>

<div class="container">

    <div class="row">

        <div class="col-md-6 box">Content</div>

        <div class="col-md-6 box">Content</div>

    </div>


</div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/
jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

小智 5

将@RestController批注更改为@Controller批注。其余控制器特定于字符串。