spring-boot-note4---freemarket

spring-boot整合freemarket

freemarker是一个模板引擎,我们在使用SpringMVC等框架的时候,使用的都是jsp,jsp的本质其实就是一个Servlet,其中的数据需要在后端进行渲染,并且每次都要渲染,而后端使用模板引擎的好处就是,渲染效率高,并且模板有些内容不需要重复渲染。本文主要介绍spring-boot整合freemarket的使用。

工程一览

spring-boot-note4---freemarket_第1张图片

一、在application.properties中配置#freemarket 

#freemarket的配置
spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/templates
spring.freemarker.request-context-attribute=req
spring.freemarker.enabled=true
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.number_format=#.##
spring.freemarker.settings.classic_compatible=true

二、使用方式

1、在controller中返回视图路径/base ,这里实际上就是 /template/base.html的模板路径

@Controller
public class IndexAction {
	
	@RequestMapping("/base")
	private String base(Model model){
		List res = new ArrayList<>();
		for (int i = 0; i < 3; i++) {
			res.add(i + "");
		}
		model.addAttribute("res", res);
		return "/base";
	}
}

2、我们没有设置model的值,但是采用初始化注入一些属性和模板指令到freemarket中测试用途。

@Configuration
public class FreemarketConfig {
	
		private static List strs = new ArrayList<>();
		
		static {
			strs.add("字符串1");
			strs.add("字符串2");
			strs.add("字符串3");
			strs.add("字符串4");
		}
		
		@Value("${project.name}")
		private String project_name;
		@Value("${project.title}")
		private String project_title;
		
		@Autowired
		protected freemarker.template.Configuration configuration;
		
		@Autowired
		protected StudentTag studentTag;
		
		/**
		 * 添加自定义标签,自定义属性等等!
		 */
		@PostConstruct
		public void setSharedVariable() throws TemplateModelException {
			configuration.setSharedVariable("project_name", project_name);
			configuration.setSharedVariable("project_title", project_title);
			configuration.setSharedVariable("strs", strs);
			configuration.setSharedVariable("studentTag", studentTag);
		}

}
@Component
public class StudentTag implements TemplateDirectiveModel {
	
    private static final String PAGE_KEY = "page";
    
    @Autowired
    private StudentService studentService;

    @Override
    public void execute(Environment environment, @SuppressWarnings("rawtypes") Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
		if (map.containsKey(PAGE_KEY)) {
			DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
			String page = map.get(PAGE_KEY).toString();

			//将数据对象转换成对应的TemplateModel
			TemplateModel model = builder.build().wrap(studentService.selectAll());
			environment.setVariable("arrs", model);

		}
		templateDirectiveBody.render(environment.getOut());
	}
   
}

3、base.html模板





freemarket基本


	
	
	
${req.contextPath}/base.html
    <#list strs as str>
  • ${str}

${project_name}

${project_title}

    <#list res as re>
  • ${re_index+1}:${re}
<@studentTag page="10">
    <#list arrs as obj>
  • ${obj_index+1}:${obj.name}
  • <#if !obj_has_next> 共有${arrs?size}最后一个用户是:${obj.name}

 

 

你可能感兴趣的:(spring-boot)