Freemaker自定义标签

新建模板类,继承TemplateDirectiveModel

public class ConsultationDirective implements TemplateDirectiveModel {

    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody){

        try {
            int siteId = Integer.valueOf(map.get("siteId").toString());
            int type = Integer.valueOf(map.get("type").toString());
            int pageNo = Integer.valueOf(map.get("pageNo").toString());
            int pageSize = Integer.valueOf(map.get("pageSize").toString());

            Pagination page = consultationDao.getJcConsultationEntityPage(siteId,type,pageNo,pageSize);
            List list = new ArrayList();
            int count = 0;
            if(page != null){
                list = page.getList();
                count = page.getTotalCount();
            }
            environment.setVariable("ConsultationList", getBeansWrapper().wrap(list));
            environment.setVariable("TotalCount", getBeansWrapper().wrap(count));
            templateDirectiveBody.render(environment.getOut());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static BeansWrapper getBeansWrapper(){
        BeansWrapper beansWrapper =
                new BeansWrapperBuilder(Configuration.VERSION_2_3_21).build();
        return beansWrapper;
    }

    @Autowired
    private ConsultationDao consultationDao;
}

配置bean容器

在freemarkerConfig中配置标签变量


		
		
		
		
			
				auto_detect
				5
				UTF-8
				UTF-8
				zh_CN
				true,false
				yyyy-MM-dd HH:mm:ss
				yyyy-MM-dd
				HH:mm:ss
				0.######
				true
				
				/WEB-INF/ftl/jeecms/index.ftl as p,/WEB-INF/ftl/spring.ftl as s
			
		
	

此处为全局变量,需要在对应的.properties中添加对应的映射

directive.cms_ConsultationDirective_list=cms_ConsultationDirective_list

使用标签访问

[@cms_ConsultationDirective_list siteId=siteId type=type pageNo=pageNo pageSize=pageSize]
            [#list ConsultationList as a]
            
  • [@text_cut s=a.theme! len='12' append='...'/] ${TotalCount}
  • [/#list] [/@cms_ConsultationDirective_list]

    或者

    <@cms_ConsultationDirective_list name="zhangsan">
        <#if userList?? && userList?size gt 0>
            <#list userList as user>
                ${user.name}
            
         
    
    

     

    你可能感兴趣的:(java学习)