Spring Boot基础
来源:互联网 发布:程序设计导论 python 编辑:程序博客网 时间:2024/05/18 03:01
Spring Boot为我们集成了不同的模板引擎,对以下的视图模板都进行了支持
- Thymeleaf
- Freemarker
- Velocity -
本文以thymeleaf进行演示,就是html文件
1.在pom文件中添加对thymeleaf的引用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.编写controller层
注意controller层要在application.java[就是带有main方法的那个文件]包同级或者下级,否则可能会出席扫描不到等异常
import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController { @RequestMapping("/") public String index(ModelMap map) { // 加入一个属性,用来在模板中读取 map.addAttribute("name", "Hello Spring Boot!!"); // return模板文件的名称,对应路径为src/main/resources/templates/index.html return "index"; }}
3.在src/main/resources/templates/目录下创建index.html
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" /> <title>test</title></head><body><h1 th:text="${name}">Hello World</h1>test</body></html>
大功告成!!
至于json返回的话,与以前的spring Mvc并无太大区,但是新增一个注解@RestController 是@controller和@ResponseBody和结合,用于类上面,表示是controller层,并且所有返回的数据会以json返回
阅读全文
0 0
- Spring boot 基础
- spring-boot 基础入门
- Spring Boot 基础
- Spring Boot基础
- Spring Boot基础
- Spring Boot基础
- Spring Boot基础
- spring boot 基础(一)
- Spring Boot 基础
- Spring Boot基础之“什么是Spring Boot”
- spring boot 基础日志配置
- Spring Boot 基础学习文章
- Spring Boot数据库基础操作
- spring boot 基础日志配置
- spring boot项目基础搭建
- Spring boot/cloud 基础文档
- Spring Boot实战之Spring基础配置
- spring boot零基础学习 spring boot简介(一)
- IP数据报格式详解
- ListView中headerview的动态显示和隐藏的问题
- 使用NavicatforMySQL导入sql文件,如果表中有中文字符就会报错
- git 常用命令
- 解决问题:安装ubuntu时出现 (initramfs)unable to find a medium containing a live file
- Spring Boot基础
- qsort函数应用大全(转)
- 总结梯度优化算法
- 关于Wrap Lighting与皮肤SSS
- PHP字符串的连接的简单实例
- 常见的排序算法
- [LeetCode] 136. Single Number
- Django 设置template的全局变量
- 程序员面试(1):利用位运算完成两个整数的加法运算