jfinal自定义freemarker标签

2018-02-08 11:49:29 浏览数 (2)

jfinal自定义freemarker标签


代码语言:javascript复制
    1. config修改freemarkerrender
    public void afterJFinalStart() {
        super.afterJFinalStart();
        FreeMarkerRender.getConfiguration().setSharedVariable("bitch",new LabelDirective());
    }
    
    2.添加对应的标签方法实现
    public class LabelDirective implements TemplateDirectiveModel {
        public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        // 真正开始处理输出内容
        List<User> users = new ArrayList<User>();
        users.add(new User(11, "a", "AAA"));
        users.add(new User(22, "b", "BBB"));
        users.add(new User(33, "c", "CCC"));
        environment.setVariable("users", ObjectWrapper.DEFAULT_WRAPPER.wrap(users));
        templateDirectiveBody.render(environment.getOut());
        }
    }
    
    3.页面调用
    <@bitch name="bitch">
        <#list users as user>
             <option value="${user.username}">${user.username}</option>
        </#list>
    </@bitch>

问题

使用<@bitch> </@bitch>标签时候 ==templateDirectiveBody== 空指针.

<></> 标签之间要有内容

0 人点赞