天天看點

03.Beetl模闆變量以及自定義模闆配置---《Beetl視訊課程》臨時變量全局變量自定義beetl配置從session中取值解決編碼錯誤共享變量ctxPath

本期視訊設定一個全局可配置的網站标題;

内容簡介:使用臨時變量、全局變量、共享變量、自定義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

部落格目錄: