Freemarker自定义指令

场景

在做一个CMS网站时,要列出网站栏目,比如有新闻,科技,体育,财经等栏目。栏目内容存放在数据库中。

调用语法

                //没有循环变量
        <@user_def_dir_exp param1=val1 param2=val2 ... paramN=valN/>

                //有循环变量
        <@user_def_dir_exp param1=val1 param2=val2 ... paramN=valN ;lv1, lv2, ..., lvN/>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

自定义指令使用

cms_chanel_list指令的作用是按传入的参数count找出指定数量的栏目,并且过滤掉名字为“体育”的栏目。

    <@cms_chanel_list count=5 exclude="体育">
        <#list chllist as c>
        <dt>
        <a href="#" target="_self"><span>${c.name}</span></a>
        </dt>
        </#list>
    </@cms_chanel_list> 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

自定定义指令

首先要实现TemplateDirectiveModel接口。

public class ChanelListDirective implements TemplateDirectiveModel {
    @Inject
    private ChanelDao chanelDao;
    /*
     * @param 传入的参数,params类型为Map<String,
     * TemplateModel>,由于历史原因没用泛型。比如传入参数“count=5”,String为count,TemplateModel为5
     * 
     * @loopVars 循环变量
     * 
     * @see freemarker.template.TemplateDirectiveModel#execute(freemarker.core.
     * Environment, java.util.Map, freemarker.template.TemplateModel[],
     * freemarker.template.TemplateDirectiveBody)
     */
    public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars,
            TemplateDirectiveBody body) throws TemplateException, IOException {
        // TODO Auto-generated method stub
        if (params == null || params.size() == 0) {
            throw new TemplateException("params can not be empty", env);
        }
        int count = 0;
        String excludeStr = null;
        // 处理传入的参数
        for (Object key : params.keySet()) {
            String name = (String) key;
            if (name.equalsIgnoreCase("count")) {
                if (params.get(key) instanceof TemplateNumberModel)
                    count = ((TemplateNumberModel) params.get(key)).getAsNumber().intValue();
                else
                    throw new TemplateException("count param must be number", env);
            }
            if (name.equalsIgnoreCase("exclude")) {
                if (params.get(key) instanceof TemplateScalarModel)
                    excludeStr = ((TemplateScalarModel) params.get(key)).getAsString();
                else
                    throw new TemplateException("execlude param must be string", env);
            }
        }
        List<Chanel> chanleList = chanelDao.loadAll();
        List<Chanel> list = new ArrayList<>();
        for (int i = 0; i < count && i < chanleList.size(); i++) {
            if (chanleList.get(i).getName().equals(excludeStr))
                continue;
            list.add(chanleList.get(i));
        }
        env.setVariable("chllist", ObjectWrapper.DEFAULT_WRAPPER.wrap(list));
        if (body != null) {
            body.render(env.getOut());
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

在freemarker中配置

需要绑定该指令为cms_chanel_list

    ...
    <bean id="chanelListDirective" class="com.ydoing.web.directives.ChanelListDirective"/>
    <!-- 配置freeMarker的模板路径 -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/t/cms/www/default/" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="freemarkerVariables">
            <map>
                <entry key="cms_chanel_list" value-ref="chanelListDirective"/>
            </map>
        </property> 
    ...
要自定义一个名为 ListDirective 的 FreeMarker 指令,你需要遵循以下步骤: 1. 创建一个 Java 类来实现指令。这个类应该继承 `freemarker.template.TemplateDirectiveModel` 接口,并且需要实现 `execute()` 方法,该方法会被 FreeMarker 引擎调用来处理指令。 ```java import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import freemarker.core.Environment; import freemarker.template.*; public class ListDirective implements TemplateDirectiveModel { public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, java.io.IOException { // 在这里编写逻辑来处理指令 // ... } } ``` 2. 在 `execute()` 方法中编写处理逻辑。在这里,你需要解析和验证指令参数,并使用 `TemplateModel` 对象来获取指令所在的模板环境和输出流。你也需要创建一个 `SimpleScalar` 对象来包装列表数据,并将其放入模板环境中。 ```java public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, java.io.IOException { // 解析和验证指令参数 String listName = DirectiveUtils.getRequiredParam(params, "name"); Object listData = DirectiveUtils.getRequiredParam(params, "data"); // 获取模板环境和输出流 Writer out = env.getOut(); PageContext pageContext = (PageContext) env.getCustomAttribute("PageContext"); if (pageContext == null) { throw new JspException("PageContext not found"); } // 创建 SimpleScalar 对象来包装列表数据 SimpleScalar listModel = new SimpleScalar(listData.toString()); // 将 SimpleScalar 对象放入模板环境中 env.setVariable(listName, listModel); // 处理指令内容 if (body != null) { body.render(out); } } ``` 3. 将自定义指令注册到 FreeMarker 引擎中。你可以使用 `Configuration` 对象的 `setSharedVariable()` 方法将指令实例添加到引擎中。 ```java import freemarker.template.Configuration; Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); cfg.setSharedVariable("list", new ListDirective()); ``` 现在,你就可以在模板中使用自定义指令了: ``` <@list name="myList" data=[1, 2, 3]> <#list myList as item> ${item} </#list> </@list> ``` 这将输出: ``` 1 2 3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值