Spring Boot 页面跳转视图解析Thymeleaf和FreeMarker详解
来源:互联网 发布:材料库存软件 编辑:程序博客网 时间:2024/06/06 09:48
最近看的springboot 在网上看到了很多教程,跳转有很多方法,在这里,我记录了三种,供大家参考
spring boot 在springmvc的视图解析器方面就默认集成了ContentNegotiatingViewResolver和BeanNameViewResolver,在视图引擎上就已经集成自动配置的模版引擎,如下:
1. FreeMarker
2. Groovy
3. Thymeleaf
4. Velocity (deprecated in 1.4)
6. Mustache
JSP技术spring boot 官方是不推荐的,原因有三:
1. 在tomcat上,jsp不能在嵌套的tomcat容器解析即不能在打包成可执行的jar的情况下解析
2. Jetty 嵌套的容器不支持jsp
3. Undertow
spring Boot加载html默认到resources/templates里寻找:
Thymeleaf
首先 增加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
1)
如 index.html.
需要注意的是:自动生成的html, 是不全的,注意区分。
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head> <meta charset="UTF-8"/> <title>Title</title></head><body>Hello world!</body></html>
Controller进行跳转,
提供了两种方式,1)是直接返回字符串,字符串为html的名字,视图会自动解析。
2)是利用ModelAndView,如图:
@Controllerpublic class TestController { @RequestMapping("/mvc1") public String mvc1(){ return "index"; } @RequestMapping("/mvc2") @ResponseBody public ModelAndView mvc2(){ ModelAndView mv = new ModelAndView("index"); return mv; } }
此时测试
localhost:8080/mvc1
成功跳转界面
FreeMarker详解
首先,增加依赖,freemarker和devtools必须有,还有web,这个自动就有,如果没有记得加上
<!--加载静态文件模板第二种方法,freemarker,一定记得加devtools--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
1)
在这里首先是demo.ftl
一定注意是ftl结尾。
<!DOCTYPE html><html><head> <meta charset="UTF-8"/> <title>Insert title here</title></head><body>请看说明:${descrip} <br /></body></html>
2)TestController
@Controllerpublic class TestController { @RequestMapping("/demo") public String demo(Map<String, Object> map) { map.put("descrip", "It's a springboot integrate freemarker's demo!!!!"); return "demo"; }}
3)浏览器测试
local:8080/demo
完美成功。当然 还有最重要的jsp。不推荐用
springboot整合jsp
阅读全文
0 1
- Spring Boot 页面跳转视图解析Thymeleaf和FreeMarker详解
- Spring boot和Mybatis整合+Mysql+jsp页面跳转问题(借助FreeMarker)
- spring-boot跳转页面
- thymeleaf+spring boot 页面hashmap取值
- spring boot与thymeleaf页面传参两种方式
- Spring Boot 学习11--使用模版Thymeleaf- freemarker
- spring-boot配置html模板引擎freemarker、thymeleaf
- [spring boot][thymeleaf]spring boot 1.5+ 页面无法找到
- spring boot(四):thymeleaf使用详解
- 《spring-boot学习》-06-thymeleaf详解
- spring boot(四):thymeleaf使用详解
- Spring Boot系列(四):thymeleaf使用详解
- spring boot(四):thymeleaf使用详解
- Spring Boot系列(四):thymeleaf使用详解
- spring boot(四):thymeleaf使用详解
- spring boot(四):thymeleaf使用详解
- 转载:Spring Boot (四):thymeleaf使用详解
- Spring boot项目整合thymeleaf和shiro
- itext使用html标签生成pdf文件
- Python学习日志(四)之模块
- hadoop搭建四个配置文件(core-site.xml hdfs-site.xml mapred-site.xml yarn-site.xml )的简单介绍
- 基于EVPN实现neutron的VxLan网络
- 万字长文剖析AliSQL X-Cluster|基于X-Paxos的高性能强一致MySQL数据库
- Spring Boot 页面跳转视图解析Thymeleaf和FreeMarker详解
- HDU1326:Box of Bricks
- 局域网虚拟机centos6.5安装mysql局域网网内访问
- swift_046(Swift map,filter, reduce方法)
- Linux 常见安全检查方法
- java-就近原则(方法重载中)
- Google I/O Android官方新体系架构之:Lifecycle
- Unity Shader 之 不锈钢(各向异性材质)
- FTPrep, 32 Longest Valid Parentheses