Freemarker 自定义标签 实现TemplateDirectiveModel

1 自定义标签需要实现TemplateDirectiveModel这个接口中的execute方法 实例代码如下

public class UserListDirective implements TemplateDirectiveModel{

    @Autowired
    private UserDAO  userDao;
    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars,
            TemplateDirectiveBody body)
                      throws TemplateException, IOException {
        String name = params.get("name").toString();
        List<User> userlist = userDao.findByProperty("name", name);

        env.setVariable("userList", getBeansWrapper().wrap(userlist));
        body.render(env.getOut());
    }

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

2 配置 UserListDirective 到spring bean xml中

<bean id="userListDirective" class="com.action.directive.UserListDirective"></bean>

3 将spring bean 设置到freemarkerConfig全局变量中去。

<bean id="freemarkerConfig2"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/" />
        <property name="freemarkerVariables">
            <map >
                <entry key="userListDirective" value="userListTag" />
            </map>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">0</prop>
                <prop key="defaultEncoding">UTF-8</prop>
                <prop key="url_escaping_charset">UTF-8</prop>
                <prop key="locale">zh_CN</prop>
                <prop key="boolean_format">true,false</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="date_format">yyyy-MM-dd</prop>
                <prop key="time_format">HH:mm:ss</prop>
                <prop key="number_format">0.######</prop>
                <prop key="whitespace_stripping">true</prop>
            </props>
        </property>
    </bean>

4 ftl文件中的访问方式

<@userListTag name="zhangsan">
    <#if userList?? && userList?size gt 0>
        <#list userList as user>
            <a href="">${user.name}</a>
        </#list>
    </#if>
</@userListTag>
时间: 2024-10-04 08:30:43

Freemarker 自定义标签 实现TemplateDirectiveModel的相关文章

freemarker自定义标签(与java合用)

自定义类继承FreemarkerManager类,重写protected Configuration createConfiguration(ServletContext servletContext)throws TemplateException方法定义有哪些TemplateDirectiveModel类与它的别名[自定义标签名称],通过Spring来取: import java.util.Map;import javax.servlet.ServletContext;import org.

freemarker自定义标签报错(八)

1.错误描述 freemarker.core.ParseException: Token manager error: freemarker.core.TokenMgrError: Unknown directive: #assgin on line: 18, column: 151, in template: tag.ftl in tag.ftl at freemarker.template.Template.<init>(Template.java:174) at freemarker.c

freemarker自定义标签报错(三)

freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered " " at line 14, column 12 in myself.ftl. Was expecting one of: <STRING_LITERAL> ... <RAW_STRING> ... "false" ... "true" ... <INTEGER> ... &l

freemarker自定义标签

freemarker自定义标签 1.自定义标签说明 宏变量存储模板片段可以被用作自定义指令macro 2.示例说明 <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>freemarker自定义标签</title> </head> <body> <#--

freemarker自定义标签报错(四)

freemarker自定义标签 1.错误描述 六月 05, 2014 11:31:35 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template processing error: "Error executing macro: write\nrequired parameter: nums is not specified." Error executing macro: write required pa

freemarker自定义标签(二)

freemarker自定义标签 1.自定义标签 通过自定义标签,写一个重复指定字符串 2.实现源码 <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>freemarker自定义标签</title> </head> <body> <#--freemark

freemarker自定义标签(三)-nested指令

freemarker自定义标签 1.nested指令 是可选的,可以在<#macro>和</#macro>之间使用在任何位置和任意次数 2.示例说明 <#macro tag> <#nested/> </#macro> <@tag> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li>

freemarker自定义标签报错(七)

1.错误描述 六月 09, 2014 11:11:09 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template processing error: "Expression a is undefined on line 27, column 23 in tag.ftl." Expression a is undefined on line 27, column 23 in tag.ftl. The probl

freemarker自定义标签报错(一)

freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Token manager error: freemarker.core.TokenMgrError: Unknown directive: #macro on line: 12, column: 101, in template: myself.ftl in myself.ftl at freemarker.template.Template.<init>(Template.java