怎么在html中写自定义freemaker标签

2025-04-12 10:05:52
推荐回答(4个)
回答1:

  1. 使用freemarker标签,必须能够被FreemarkerServlet拦截到,然后解析处理成html所以你直接用ftl页面就行
    直接使用java代码就可以了

  2. Java代码  

  3. 在页面中加入下列一行即可:  

  4. <#global hm=JspTaglibs["/WEB-INF/hymake.tld"]><#global ww=JspTaglibs["/WEB-INF/webwork.tld"]>  

  5. 写成跟宏类似就行,以@开头:  

  <@ww.select label="aa" name="bb" list="{'en_US','zh_CN'}"/>  

回答2:

package com.lenxeon.extjs.cms.directive;
 
import com.lenxeon.extjs.cms.bean.Channel;
import com.lenxeon.extjs.cms.bean.HotWord;
import com.lenxeon.extjs.cms.dao.ChannelService;
import com.lenxeon.extjs.cms.dao.HotWordService;
import com.lenxeon.extjs.cms.utils.ParamUtils;
import com.lenxeon.extjs.resource.bean.Article;
import com.lenxeon.extjs.resource.dao.ArticleService;
import freemarker.core.Environment;
import freemarker.template.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class ArticleDirective implements TemplateDirectiveModel {
 
    @Autowired
    private ArticleService articleService;
 
    @Autowired
    private ChannelService channelService;
 
    @Autowired
    private HotWordService hotWordService;
 
    @Override
    public void execute(Environment env, Map params, TemplateModel[] templateModels, TemplateDirectiveBody body) throws TemplateException, IOException {
        int rid = ParamUtils.getIntValue(params, "rid");
        Article article = articleService.findById(rid);
        List channels = channelService.listByResource(rid);
        if (channels != null && channels.size() > 0) {
            env.setVariable("channel", ObjectWrapper.DEFAULT_WRAPPER.wrap(channels.get(0)));
        }
        if (true) {
            if (article.getContent() != null && StringUtils.isNotBlank(article.getContent().getContent())) {
                Map map = new HashMap();
                map.put("siteId", 1);
                List hotWords = hotWordService.list(map);
                String content = StringUtils.defaultString(article.getContent().getContent());
                for (HotWord hotWord : hotWords) {
                    content = replace(content, hotWord);
                }
                article.getContent().setContent(content);
            }
        }
        env.setVariable("article", ObjectWrapper.DEFAULT_WRAPPER.wrap(article));
        body.render(env.getOut());
    }
 
    private String replace(String data, HotWord hotWord) {
        try {
            if (hotWord != null) {
                if (hotWord.getType() == 0 && StringUtils.isNotBlank(hotWord.getName())) {
                   data = StringUtils.replace(data, hotWord.getName(), hotWord.getReplaceTo());
                }
                if (hotWord.getType() == 1 && StringUtils.isNotBlank(hotWord.getName())) {
                    String html = ""
                            + hotWord.getName()
                            + "";
                    data = StringUtils.replace(data, hotWord.getName(), html);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
 
}

回答3:

要想使用freemarker标签,必须能够被FreemarkerServlet拦截到,然后解析处理成html所以直接用ftl页面就行了,为什么要静态网页和动态模版混着用呢 如果真想用。。web.xml 中freemarker拦截器(制定拦截*.html的页面

回答4:

想使用freemarker标签,必须能够被FreemarkerServlet拦截到,然后解析处理成html所以你直接用ftl页面就行了,为什么要静态网页和动态模版混着用呢 如果真想用。web.xml 中freemarker拦截器(制定拦截*.html的页面)