03.Beetl模板變量以及自定義模板配置---《Beetl視頻課程》

本期視頻設(shè)置一個(gè)全局可配置的網(wǎng)站標(biāo)題;

內(nèi)容簡(jiǎn)介:使用臨時(shí)變量、全局變量、共享變量、自定義Beetl配置、使用ctxPath解決亂碼、404等問(wèn)題

一起學(xué)beetl目錄:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598

作者:GK


臨時(shí)變量

在模板中定義的變量成為臨時(shí)變量,這類(lèi)似js中采用var 定義的變量,如下例子

<%
var a = "xxxx";

%>

全局變量

全局變量是通過(guò)template.binding傳入的變量,這些變量能在模板的任何一個(gè)地方,包括子模板都能訪問(wèn)到。如java代碼里

template.binding("list",service.getUserList());

//在模板里
<%
for(user in list){
%>
hello,${user.name};
<% } %>

在請(qǐng)求中beetl會(huì)從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網(wǎng)站");
        return "index1.html";
    }

共享變量
共享變量指在所有模板中都可以引用的變量,可通過(guò)groupTemplate.setSharedVars(Map<String, Object> sharedVars)傳入變量,這些變量能用在 所有模板 的任何一個(gè)地方

GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Map<String,Object> shared = new HashMap<String,Object>();
shared.put("name", "beetl");
gt.setSharedVars(shared);

哪怎么去獲取GroupTemplate對(duì)象呢?我們可以自定義一個(gè)Beetl配置。然后設(shè)置我們要的值。

自定義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();
        //如果使用了優(yōu)化編譯器,涉及到字節(jié)碼操作,需要添加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中一樣,只不過(guò)前面加一個(gè)session

${session.title}

解決編碼錯(cuò)誤

修改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默認(rèn)共享變量ctxPath表示 Web應(yīng)用ContextPath

可以用解決路徑問(wèn)題,如 圖片、樣式無(wú)法找到的問(wèn)題


項(xiàng)目git地址:https://gitee.com/gavink/beetl-blog

視頻地址:下載下來(lái)會(huì)更清晰

百度網(wǎng)盤(pán)下載: https://pan.baidu.com/s/1LyxAxlKpVXgVjwSXIbzBuA 提取碼: 68im

bilibili (可以調(diào)節(jié)清晰度): https://www.bilibili.com/video/av36278644/?p=3

博客目錄:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容