IDEA从无到有搭建Springboot Hello Web

爱⌒轻易说出口 提交于 2019-11-30 23:30:51

Springboot Hello Web

最近在学习通过springboot写一个hello页面,通过百度以及结合项目中的使用,Mark一下其实现过程。本文通过IDEA实现。

创建项目

  1. 打开IDEA,点击 [File] ->[New] ->[Project…](如下图)。
    在这里插入图片描述
  2. 创建项目(如下图)
    在这里插入图片描述
    在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

到此项目已近创建完毕。创建完成后,其结构如下:
在这里插入图片描述

接下来进行代码实现。

创建Controller类

创建Controller类,其内容如下

@Controller
public class HelloController {
    @RequestMapping(value = "/hello")
    public ModelAndView sayHello(){
        return  new ModelAndView("/hello");
    }
}

修改Springboot启动类,在该java文件里面添加@ComponentScan注解,其内容如下所示:

@SpringBootApplication
@ComponentScan("com.demo")
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

在templates里面添加hello.html文件,其内容如下:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <meta charset="UTF-8">
    <!-- 页面标题-->
    <title>Springboot</title>
    <!-- 替换默认图标-->
    <link th:href="@{icon.png}"  rel="shortcut icon" type="image/x-icon">
</head>
<body>
HELLO
</body>
</html>

到此,代码已经实现或修改完毕。

启动springboot(如下图)

在这里插入图片描述

当springboot启动完成后,在浏览器输入http://localhost:8080/hello,将在页面显示HELLO内容。如下图:
在这里插入图片描述

完结:
通过简单的步骤,一个简单的springboot web hello页面搭建成功。相比通过spring mvc来搭建,效率不止快一倍。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!