如何实现自定义freemarker标签

每次遇到不懂的,亦或是写没写过的效果,总要到网上搜搜,虽然有时没有具体的解决办法,但是总能给我不少的灵感,所以网友们的博客文章真的给了我特别大的帮助,我也总觉得,我也应该分享分享我所知道的经验,为我们苦逼的程序员,大家共同学习,共同进步,好了废话不多说了,我今天就教教大家如何使用freemarker的自定义标签吧!希望大家共同的学习,如果有什么错误的地方,亦或是不懂的地方,欢迎加扣一起探讨! 2739677415
1:首先当然是要在springMvc配置文件中配置了freemarker视图解析器,如果不知道的朋友,可以去网上查查,这方面的资料网上挺多的,然后就是新建一个自定义标签类,继承freemarker模板类TemplateMethodModel,和spring容器类ApplicationContextAware ,然后实现TemplateMethodModel的exec方法,下面是具体的代码实现。
package com.fanli.mall.platform.freemarker;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.fanli.mall.vo.ClassifyVo;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

public class ClassifyActivesUtil implements TemplateMethodModel,
ApplicationContextAware {
private ApplicationContext context;
@Autowired
private HttpServletRequest request;
@Override
public Object exec(List args) throws TemplateModelException {
String appContext = request.getContextPath();
String basePath = request.getScheme() + “://” + request.getServerName()
+ “:” + request.getServerPort() + appContext;
Integer classifyId = Integer.valueOf(args.get(0).toString());

    StringBuffer sb = new StringBuffer();
    Map<Integer, List<ClassifyVo>> map = (Map<Integer, List<ClassifyVo>>) request
            .getServletContext().getAttribute("classifyVos");

    List<ClassifyVo> list = map.get(classifyId);
    int i = 0;
    if (list != null && list.size() > 0) {
        for (ClassifyVo classifyVo2 : list) {
            if (i <= 5) {
                if (i == 0) {
                    sb.append("<span><a href=\"javascript:void(0)\" class=\"hover\" name=\"searchKeys\" acValue="
                            + classifyVo2.getClassifyValue()
                            + ">"
                            + classifyVo2.getClassifyName() + "</a></span>");
                } else {
                    sb.append("<span><a href=\"javascript:void(0)\"  name=\"searchKeys\" acValue="
                            + classifyVo2.getClassifyValue()
                            + ">"
                            + classifyVo2.getClassifyName() + "</a></span>");
                }
            } else {

                sb.append("<span><a href=\"" + basePath
                        + "/active/moreActives.do\"  >更多</a></span>");
                break;
            }   
            i++;
        }
    }
    sb.append("   </div>");
    return sb.toString();
}
@Override
public void setApplicationContext(ApplicationContext context)
        throws BeansException {
    this.context = context;
}
public ApplicationContext getContext() {
    return context;
}

}

2:接下来就是在springmvc配置文件中,freemarker视图解析器下配置属性attributesMap,这个标签的下可以配置多个freemarker自定义标签,attributesMap标签下首先是个map子标签,相信大家看到map就已经知道的它的意思了,map下的entry 标签就是你自定义的方法名,entry 下的bean就是具体的实现类了,和上面的类对应,就是引入自定义类的类路径了。

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="requestContextAttribute" value="rc" />
        <property name="cache">
            <value>false</value>
        </property>
        <property name="suffix">
            <value>.html</value>
        </property>
        <!-- 自定义FreeMarkerView,用来定义项目的全局路径 -->
        <property name="viewClass" value="com.fanli.mall.platform.freemarker.BasePathFreeMarkerView" ></property>
        <property name="prefix">
            <value>/WEB-INF/</value> 
        </property>
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="contentType">
            <value>text/html; charset=UTF-8</value>
        </property>
        <property name="exposeRequestAttributes" value="true"></property>
        <property name="exposeSessionAttributes" value="true"></property>
        <property name="allowSessionOverride" value="true"></property>
        <property name="allowRequestOverride" value="true"></property>
         <property name="attributesMap">  
        <map>  
            <!-- 定义Freemarker方法的名称 -->  
            <entry key="classifySelect">  
                <!-- 关联到我们之前定义的工具类 -->  
                <bean id="classifySelectUtil" class="com.fanli.mall.platform.freemarker.ClassifySelectUtil" />  
            </entry>  
            <entry key="classifyGetItems">  
                <!-- 关联到我们之前定义的工具类 -->  
                <bean class="com.fanli.mall.platform.freemarker.ClassifyGetItemNamesUtil" />  
            </entry>                  
        </map>  
    </property>  
    </bean>

3:最后的最好就怎么使用自定义标签了,这个我相信大家都懂,因为这个和我调用方法一样一样的,下面给出具体的代码。可供参考

${classifyArticleUtil(classify.classify_article_type,'${basePath}/article/items.do','zixun01b1')}

4:肯能有的朋友看了上面的代码,有一处还不是还明白,

Map<Integer, List<ClassifyVo>> map = (Map<Integer, List<ClassifyVo>>) request
                .getServletContext().getAttribute("classifyVos");

对这个数据是什么时候存入request作用域的,其实这是spring容器启动的时候自动加载到spring容器中的,
这个需要在web.xml中配置ServletContextListener,具体的就不说了,网上一搜一大把的,好了,今天的分享就到这里,下次有好的经验还会继续分享的,也希望大家也能够踊跃的行动起来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值