省略创建springboot项目的步骤
配置pox.xml添加freemarker依赖
com.ibeetl
beetl-framework-starter
1.1.70.RELEASE
法一:
starter 自动处理以btl结尾的视图,模板根目录是Spring Boot默认的templates目录。
这样看来可以直接使用
创建beetl页面userInfo.btl 当然也可用直接用html,以btl为后缀是为了区分其他模板语言,也为了让启动以来直接处理。
Title
${name}
${id}
编写BeetlController
@Controller
@RequestMapping("beetl")
public class BeetlController {
@RequestMapping("/showuser.html")
public ModelAndView showUsername(String id) {
ModelAndView mv = new ModelAndView();
mv.addObject("id", id);
mv.addObject("name", "dongguo1");
mv.setViewName("/userInfo.btl");
return mv;
}
}
application.yml中的配置
server:
port: 8080
servlet:
context-path: /springboot-web
访问页面
http://localhost:8080/springboot-web/beetl/showuser.html?id=456
法二(转 https://blog.csdn.net/xslde_com/article/details/81132673):
使用Starter来配置已经够用,如果你想自己配置模板引擎, 通过java config来配置 beetl需要的BeetlGroupUtilConfiguration,和 BeetlSpringViewResolver,
创建beetl配置文件:BeetlConf.class
package com.xslde.configurer;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author xslde
* @Description
* @Date 2018/7/20 15:17
*/
@Configuration //加载配置文件
public class BeetlConf {
@Value("${beetl.templatesPath}") String templatesPath;//模板根目录 ,比如 "templates"
@Bean(name = "beetlConfig")
public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
//获取Spring Boot 的ClassLoader
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if(loader==null){
loader = BeetlConf.class.getClassLoader();
}
//beetlGroupUtilConfiguration.setConfigProperties(extProperties);//额外的配置,可以覆盖默认配置,一般不需要
ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader,
templatesPath);
beetlGroupUtilConfiguration.setResourceLoader(cploder);
beetlGroupUtilConfiguration.init();
//如果使用了优化编译器,涉及到字节码操作,需要添加ClassLoader
beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);
return beetlGroupUtilConfiguration;
}
@Bean(name = "beetlViewResolver")
public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
beetlSpringViewResolver.setOrder(0);
beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
return beetlSpringViewResolver;
}
}
在application.yml中配置模板引擎路径:
server:
port: 8080
servlet:
context-path: /springboot-web
#模板路径
beetl:
templatesPath: templates
创建一个 BeetlAction.class
package com.xslde.action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @Author xslde
* @Description
* @Date 2018/7/20 15:22
*/
@Controller
public class BeetlAction {
@RequestMapping("/index")
public String beetl(Model model) {
model.addAttribute("beetl", "测试一下通过模板引擎传递参数!");
return "index.html";
}
}
在templates文件夹下创建一个html文件
index.html
测试页面
通过beetl取值
${beetl!}
访问页面
http://localhost:8080/springboot-web/beetl/index