admin管理员组文章数量:1794759
SpringBoot之模板
重点: 0、回顾; 1、SpringBoot之thymeleaf模板 2、springboot之freemarker模板
重点解析: 0、回顾加补充 0.1、springboot入门中新建spring boot项目时三个坑: (0.1.1)、必须联网,不然无法填写项目名,无法完成项目创建。 (0.1.2)、创建springboot项目名的时候是需要小写的,不然无法进行下一步。 (0.1.3)、在进行项目创建最后一步的时候不要掉以轻心。因为第一遍可能必须创建成功。将项目删除之后重新创建一次基本上就没有多大的问题。如果在pom.xml里面还是报错,就在本地仓库找到相关依赖删除,然后重新下载一遍也可以。
0.2、application回顾 0.2.1、内置属性,直接在applocation里面添加 server.port server.servlet.context-path: 0.2.2、自定义属性在application声明,但是必须是 key=value mysql.url mysql.driver 在java里面必须要通过@value("${key}"),获取里面的值 0.2.3、属性封装类 注意必须要在实体类做添加@component和 @configuationProperties(“prefix=mysql”)
springboot模板spring boot模块共4个:Thymeleaf、Freemarker、Mustache、Groovy Templates。 前面两个使用率比较高,基本后面两个没什么人使用,就不进行进解了。如果要使用springboot模块请点击Template Engines如下面图中进行选择模块。
1、springboot之thymeleaf模板 关于Thymeleaf的优点,我只说一条:它就是html页面。下面直接上代码 相关pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
spring.thymeleaf.cache=false正式环境还是要将缓存开启的
<html xmlns:th="www.thymeleaf">实体类方法:user.java
public class User { private Integer uid; private String uname; private String upwd; public User(){ } public User(Integer uid, String uname, String upwd) { this.uid = uid; this.uname = uname; this.upwd = upwd; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } }代码分享:IndexController.java
@RequestMapping("/user/list") public ModelAndView userlist(){ ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("/user/list"); modelAndView.addObject("title","用户列表"); List<User> list=new ArrayList<>(); list.add(new User(1,"张晨光","6789")); list.add(new User(2,"韩伊婷","5675678")); modelAndView.addObject("users",list); return modelAndView; }代码分享:list.html
<!DOCTYPE html> <html lang="en"> <html xmlns:th="www.thymeleaf"> <head> <meta charset="UTF-8"> <title th:text="${title}"></title> </head> <body> <!-- text 用于展示--> <h1 th:text="${title}"></h1> <!-- each 类似于foreach ,用来循环输出 --> <table border="1px" width="600px"> <thead> <tr> <td>id</td> <td>用户名</td> <td>密码</td> </tr> </thead> <tbody> <tr th:each="user :${users}"> <td th:text="${user.uid}">id</td> <td th:text="${user.uname}">用户名</td> <td th:text="${user.upwd}">密码</td> </tr> </tbody> </table> <select> <option th:each="user:${users}" th:value="${user.uid}" th:text="${user.uname}" ></option> </select> </body> </html> 运行效果图:2、springboot之freemarker模板 导入pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>application.yml文件的默认配置
spring: thymeleaf: cache: false freemarker: # 设置模板后缀名 suffix: .ftl # 设置文档类型 content-type: text/html # 设置页面编码格式 charset: UTF-8 # 设置页面缓存 cache: false # 设置ftl文件路径,默认是/templates,为演示效果添加role template-loader-path: classpath:/templates/role mvc: static-path-pattern: /static/**IndexController.java代码分享:
@RequestMapping("/role/list") public ModelAndView rolelist() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("list"); modelAndView.addObject("name",“嘿嘿”); modelAndView.addObject("sex","boy"); List<User> list=new ArrayList<>(); list.add(new User(1,"张晨光","6789")); list.add(new User(2,"韩伊婷","5675678")); modelAndView.addObject("users",list); return modelAndView; } @RequestMapping("/role/login") public String rolelogin() { return "login"; }common.ftl代码分享:
<#-- assign 设置全局变量--> <#assign ctx> ${springMacroRequestContext.contextPath} </#assign> <base href="${ctx}/"> <script type="text/javascript" src="${ctx}/static/js/xx.js"></script>foot.ftl代码分享:
版本号 <#-- 第一个无法点击进去,因为springboot不允许直接点击,必须进行跳转才可以跳转进去 --> <a href="login.ftl">登陆1</a> <a href="${springMacroRequestContext.contextPath}/role/login">登陆2</a>login.ftl代码分享:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> 登陆 </body> </html>list.ftl 代码 :
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>角色列表</title> <#-- 静态页面的调用--> <#include 'common.ftl'> </head> <body> <h1>取值</h1> <#-- 取值里面必须有值,如果没有或者是null就直接显示500。解决方法 ${值 ! '空'} ,这里的!就代表判断,后面的空是进行替换--> welcome 【${name ! '空'}】 to page <h1>非空判断</h1> <#if name?exists> 里面有值 </#if> <h1>条件表达式</h1> <#if sex=='boy'> 男 <#elseif sex=='girl'> 女 <#else> </#if> <h1>循环</h1> <table border="1px" width="600px"> <thead> <tr> <td>id</td> <td>用户名</td> <td>密码</td> </tr> </thead> <tbody> <#list users as user> <tr> <td>${user.uid}</td> <td>${user.uname}</td> <td>${user.upwd}</td> </tr> </#list> </tbody> </table> <h1>include</h1> <#include 'foot.ftl'> <h1>变量(局部,全局)</h1> <#assign ctxl> ${springMacroRequestContext.contextPath} </#assign> <#global ctx2> ${springMacroRequestContext.contextPath} </#global> ${ctxl}.${ctx2} </body> </html> 结果效果图: ps:如何配置 .ftl的界面第一步:点击setting,进入设置然后点击 file and code Templates ,注意左边出来的是includes,要点击files。 第二步:将html里面的内容进行复制,然后点击 +,将在html里面复制的内容,粘贴在最大空白里。注意要填写name,就是创建 .ftl的项目名称。e’xtension里面填写项目的后缀名。 第三步:点击Apply,就可以在新建里面了
本文标签: 模板SpringBoot
版权声明:本文标题:SpringBoot之模板 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686786127a102708.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论