03.Beetl模板变量以及自定义模板配置---《Beetl视频课程》
本期视频设置一个全局可配置的网站标题;
内容简介:使用临时变量、全局变量、共享变量、自定义Beetl配置、使用ctxPath解决乱码、404等问题
一起学beetl目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598
作者:GK
临时变量
在模板中定义的变量成为临时变量,这类似js中采用var 定义的变量,如下例子
<%
var a = "xxxx";
%>
全局变量
全局变量是通过template.binding传入的变量,这些变量能在模板的任何一个地方,包括子模板都能访问到。如java代码里
template.binding("list",service.getUserList());
//在模板里
<%
for(user in list){
%>
hello,${user.name};
<% } %>
在请求中beetl会从request->attributes中获取变量作为模板变量,所以下面的page,blogSiteTitle也是全局变量
@GetMapping("/")
public String index(@RequestParam(required = false, defaultValue = "1") Integer pageNumber,
@RequestParam(required = false, defaultValue = "8") Integer pageSize,
HttpServletRequest request) {
PageQuery<Blog> pageQuery = blogService.pageBlog(pageNumber, pageSize);
request.setAttribute("page", pageQuery);
request.setAttribute("blogSiteTitle", "XXX网站");
return "index1.html";
}
共享变量
共享变量指在所有模板中都可以引用的变量,可通过groupTemplate.setSharedVars(Map<String, Object> sharedVars)
传入变量,这些变量能用在 所有模板 的任何一个地方
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Map<String,Object> shared = new HashMap<String,Object>();
shared.put("name", "beetl");
gt.setSharedVars(shared);
哪怎么去获取GroupTemplate对象呢?我们可以自定义一个Beetl配置。然后设置我们要的值。
自定义beetl配置
package com.ibeetl.blog.config;
import com.ibeetl.starter.BeetlTemplateConfig;
import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* @author GavinKing
* @ClassName: BeetlConfig
* @Description:
* @date 2018/11/22
*/
@Configuration
public class BeetlConfig {
//模板根目录 ,比如 "templates"
@Value("${beetl.templatesPath}") String templatesPath;
@Value("${blog.title}") String title;
@Bean
public GroupTemplate getGroupTemplate(BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
GroupTemplate gt = beetlGroupUtilConfiguration.getGroupTemplate();
Map<String,Object> shared = new HashMap<>();
shared.put("blogSiteTitle", title);
gt.setSharedVars(shared);
return gt;
}
@Bean
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
//获取Spring Boot 的ClassLoader
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if(loader==null){
loader = BeetlConfig.class.getClassLoader();
}
ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,
templatesPath);
beetlGroupUtilConfiguration.setResourceLoader(cploder);
beetlGroupUtilConfiguration.init();
//如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);
return beetlGroupUtilConfiguration;
}
@Bean(name = "beetlViewResolver")
public BeetlSpringViewResolver getBeetlSpringViewResolver(BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
return beetlSpringViewResolver;
}
}
从session中取值
从session中取值和request中一样,只不过前面加一个session
${session.title}
解决编码错误
修改SpringBoot的 application.properties配置文件,增加编码的配置
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8
共享变量ctxPath
Beetl默认共享变量ctxPath表示 Web应用ContextPath
可以用解决路径问题,如 图片、样式无法找到的问题
项目git地址:https://gitee.com/gavink/beetl-blog
视频地址:下载下来会更清晰
百度网盘下载: https://pan.baidu.com/s/1LyxAxlKpVXgVjwSXIbzBuA 提取码: 68im
bilibili (可以调节清晰度): https://www.bilibili.com/video/av36278644/?p=3
博客目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
02.Beetl模板的基础用法 【变量、循环、条件】---《Beetl视频课程》
本期视频做了一个博客的首页列表; 内容简介:springboot 集成 beetlsql;使用for循环,使用if控制语句,使用虚拟属性,定义变量等等 一起学beetl目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598 作者:GK 集成BeetlSql,用来查询数据库 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency> <dependency> <groupId>...
-
下一篇
04.Beetl常用内置函数以及安全输出---《Beetl视频课程》
本期视频实现了三个功能,模糊搜索、标签分类、登录/注销功能; 内容简介:使用了常用内置函数,以及安全输出,实现了上面提到的三个业务 一起学beetl目录:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598 作者:GK 常用内置方法 date 返回一个java.util.Date类型的变量,如 date() 返回一个当前时间(对应java的java.util.Date); ${date( "2011-1-1" , "yyyy-MM-dd" )} 返回指定日期 print 打印一个对象 print(user.name); println 打印一个对象以及回车换行符号,回车换号符号使用的是模板本身的,而不是本地系统的.如果仅仅打印一个换行符,则直接调用println() 即可 printFile 直接答应文件,文件路径以模板根目录为相对目录,printFile(‘‘/common/header.html’’); nvl 函数nvl,如果对象为null,则返回第二个参数,否则,返回自己 nvl(user,"不存在"...
相关文章
文章评论
共有0条评论来说两句吧...