idea 2020+SpringBoot 创建hello world以及简单的模板渲染
2021/4/7 10:42:44
本文主要是介绍idea 2020+SpringBoot 创建hello world以及简单的模板渲染,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1. 准备工作
接上期的idea2020 创建Spring boot工程项目以及依赖项等注意事项 https://www.cnblogs.com/cybg/p/14623027.html
确保工程项目结构完整的前提下开始正式正式创建hello world
2. 创建hello word
hello world 类文件属于自行创建的类文件,位置如上图。源码如下
package com.example.study3; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class helloword { @GetMapping("/hello") public String hello(){ return "hello word"; } }
使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面,返回的就是普通字符串,也就是hello world
3. 简单的模板语句渲染html界面
新建类如下所示
源码如下
package com.example.study3; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String home(){ return "home"; } }
使用 @Controller注解则返回的是一个名为 hello.html的页面,鼠标移到hello上会发现显示的类型为html类型,
@GetMapping("/")这里可以自定义链接地址,运行然后复制端口即可,如下图新建html文件
3. 总结截图
运行地址:http://localhost:8080/hello 即可看到效果。
这篇关于idea 2020+SpringBoot 创建hello world以及简单的模板渲染的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性
- 2024-05-29哪些无用敏捷指标正在破坏敏捷转型?
- 2024-05-29鸿蒙原生应用再新丁!新华社 入局鸿蒙
- 2024-05-29设计模式 之 迭代器模式(Iterator)