本期視頻設置一個全局可配置的網站標題睬罗;
內容簡介:使用臨時變量、全局變量报账、共享變量纵势、自定義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();
//如果使用了優(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中一樣获高,只不過前面加一個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 (可以調節(jié)清晰度): https://www.bilibili.com/video/av36278644/?p=3
博客目錄:https://my.oschina.net/u/1590490?tab=newest&catalogId=6214598