IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

创建步骤:

1、点击File->New->Project

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

2、选择Spring Initializr,以及JDK版本,博主使用的1.8版本,点击Next

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

3、Group表示路径名,Artifact表示项目名,根据需要修改即可。然后继续点击Next

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

4、点击左侧栏中Web,勾选右侧栏中Web,然后继续点击Next下一步

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

5、Project location为项目所在路径,根据自己放置位置选取。点击Finish完成。

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

6、建好后的目录,如果没显示出来的可能是下载JAR包中,等待一会即可。

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

7、.mvn、mvnw、mvnw.cmd是可以删除的,博主一般建好后习惯删除。

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

8、新建DemoController

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

9、DemoController内容如下

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

    @RequestMapping("/index")
    @ResponseBody
    public String index(){
        return "Hello World";
    }
}

10、点击Debug运行

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

11、控制台信息有Spring字样,且没有报错,表示启动成功。

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

12、根据该地址访问即可,端口默认是8080,可以在application.properties中修改端口。

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

13、现在文字的返回成功,开始配置跳转页面。

14、在pom.xml加入视图解析器模块

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

15、注意首次导入右侧会弹出需要导入JAR包,选择Import Changes导入JAR包后,下次导入新的JAR包还会提示,选择Enable Auto-Import下次有新的的Maven JAR包会自动导入。

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

16、在templates中新建HTML File     test.html

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面

17、test.html 内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 style="color: red">Hello World</h1>
</body>
</html>

18、现在去DemoController添加页面跳转的方法test()

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

    @RequestMapping("/index")
    @ResponseBody
    public String index(){
        return "Hello World";
    }

    @RequestMapping("/test")
    public String test(){
        return "test";
    }
}

19、重新Debug启动DemoApplication,具体请看步骤10

20、使用http://127.0.0.1:8080/test访问,效果如下,证明页面跳转成功。

IDEA新建Spring-boot项目,并配置thymeleaf跳转HTML页面


       好了,以上就是Spring-boot项目新建以及页面跳转的Demo,是不是很简单啊。不用去找大量的JAR包,也不用在配置文件中写一系列的配置,用起来十分方便。