临时变量

在模板中定义的变量成为临时变量,这类似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

11-25 17:13