Hello World
一、创建Maven工程
1. 新建工程
- 新建maven工程,点击Next

 - 点击Finish

 
2. 进行相应的配置
- 在pom.xml配置文件中配置打包方式。
 
	<groupId>com.znb.springMVC</groupId>
    <artifactId>SpringMvc01-deom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
 
- 在pom.xml配置文件中引入依赖
 
<dependencies>
    <!-- SpringMVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.1</version>
    </dependency>
    <!-- 日志 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
    <!-- ServletAPI -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- Spring5和Thymeleaf整合包 -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.12.RELEASE</version>
    </dependency>
</dependencies>
 
- 新建webapp包

 - 点击Project Structure进行配置

 - 选择相应的项目文件进行配置

 - 依次点击OK,Apply,OK

 
3. 配置web.xml文件
-  
在resources中新建springMVC.xml文件

 -  
web.xml
 
<?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_4_0.xsd"
         version="4.0">
    <!--配置SpringMVC的前端控制器,对浏览器发送的请求进行统一的处理-->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置SpringMVC的位置和名称-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <!--将前端控制器DispatcherServlet的初始化时间提前到服务器启动时-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!--
            / :匹配除了 .jsp以外的所有请求
            /*:匹配所有的请求
        -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
 
二、创建相应的类和配置文件
- HelloController类
 
@Controller
public class HelloController {
}
 
- springMVC配置文件
 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--扫描组件-->
    <context:component-scan base-package="com.znb.springMVC.controller"></context:component-scan>
    <!-- 配置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>
</beans>
 
- 视图前缀和视图后缀共同进行视图解析。给方法的返回值添加视图前缀和视图后缀。如返回index-/WEB-INF/templates/index.html
 
三、创建前端页面
1. 在WEB-INF在创建templates文件夹

2. 创建index.html和hello.html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/hello}">访问目标页面hello.html</a>
</body>
</html>
 
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
HelloWord
</body>
</html>
 
- 控制类的方法创建
 
@Controller
public class HelloController {
    @RequestMapping("/")
    public String index(){
        //返回视图名称
        return "index";
    }
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}
 
四、配置tomcat进行测试
- Edit配置

 - Edit配置2

 - 启动服务器进行测试

 - 点击进行页面跳转

 










