1.创建maven工程
    image.png
    image.png

    2.在pom.xml中引入依赖
    image.png

    1. <!--引入的-->
    2. <!--web工程打包方式为war-->
    3. <packaging>war</packaging> <!---->
    4. <dependencies>
    5. <!-- SpringMVC -->
    6. <dependency>
    7. <groupId>org.springframework</groupId>
    8. <artifactId>spring-webmvc</artifactId>
    9. <version>5.3.1</version>
    10. </dependency>
    11. <!-- 日志 -->
    12. <dependency>
    13. <groupId>ch.qos.logback</groupId>
    14. <artifactId>logback-classic</artifactId>
    15. <version>1.2.3</version>
    16. </dependency>
    17. <!-- ServletAPI -->
    18. <dependency>
    19. <groupId>javax.servlet</groupId>
    20. <artifactId>javax.servlet-api</artifactId>
    21. <version>3.1.0</version>
    22. <scope>provided</scope>
    23. </dependency>
    24. <!-- Spring5和Thymeleaf整合包 -->
    25. <dependency>
    26. <groupId>org.thymeleaf</groupId>
    27. <artifactId>thymeleaf-spring5</artifactId>
    28. <version>3.0.12.RELEASE</version>
    29. </dependency>
    30. </dependencies>
    31. <!--引入结束-->

    3.给当前模块添加web文件夹
    image.png
    image.png

    4.将web文件夹放到main文件夹下
    image.png

    5.配置web.xml文件

    6.创建springMVC.xml文件
    image.png

    7.创建控制器(在java文件夹下创建控制器类)
    image.png

    8.在SpringMVC.xml 中配置
    image.png
    视图解析器代码:

     <!-- 配置Thymeleaf视图解析器 -->
        <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
            <property name="order" value="1"/>
            <property name="characterEncoding" value="UTF-8"/>
            <property name="templateEngine">
                <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                    <property name="templateResolver">
                        <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                            <!-- 视图前缀 -->
                            <property name="prefix" value="/WEB-INF/templates/"/>
                            <!-- 视图后缀 -->
                            <property name="suffix" value=".html"/>
                            <property name="templateMode" value="HTML5"/>
                            <property name="characterEncoding" value="UTF-8"/>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    

    9.在WEB-INF下创建templates文件夹
    然后在此文件夹下就可以放入html文件了(这里创建index.html)
    image.png
    image.png
    index.html文件的头中要加入thymeleaf的命名空间
    xmlns:th=”http://www.thymeleaf.org
    image.png

    10.最后就可以在控制器类中写一个控制器方法,用来设置访问首页
    image.png

    11.部署到Tomcat服务器