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以及简单的模板渲染的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程