Springboot Hello Web
最近在学习通过springboot写一个hello页面,通过百度以及结合项目中的使用,Mark一下其实现过程。本文通过IDEA实现。
创建项目
- 打开IDEA,点击 [File] ->[New] ->[Project…](如下图)。
- 创建项目(如下图)
到此项目已近创建完毕。创建完成后,其结构如下:
接下来进行代码实现。
创建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来搭建,效率不止快一倍。