在ofbiz中扩展自己的FreeMarker标签和函数

首先,要先增加一个实现TemplateMethodModel接口的类,代码如下(以下代码从网上随便摘的):

[java]  view plain copy
  1. import java.util.List;  
  2. import javax.servlet.http.HttpServletResponse;  
  3. import freemarker.template.TemplateMethodModel;  
  4. import freemarker.template.TemplateModelException;  
  5.   
  6. /** 
  7.  * Freemarker自定义方法 
  8.  * 实现response.encodeURL(url)功能 
  9.  */  
  10. public class EncodeURLMethod implements TemplateMethodModel {  
  11.       
  12.     private HttpServletResponse response;  
  13.       
  14.     /** 
  15.      * 带参数的构造函数 
  16.      * @param response HttpServletResponse对象 
  17.      */  
  18.     public EncodeURLMethod(HttpServletResponse response)  
  19.     {  
  20.         this.response=response;  
  21.     }  
  22.   
  23.     /** 
  24.      * 执行方法 
  25.      * @param argList 方法参数列表 
  26.      * @return Object 方法返回值 
  27.      * @throws TemplateModelException 
  28.      */  
  29.     public Object exec(List argList) throws TemplateModelException {  
  30.         if(argList.size()!=1)   //限定方法中必须且只能传递一个参数  
  31.         {  
  32.             throw new TemplateModelException("Wrong arguments!");  
  33.         }  
  34.         //返回response.encodeURL执行结果  
  35.         return response.encodeURL((String)argList.get(0));  
  36.     }  
  37. }  

接着,找到ofbiz目录下的framework/webapp/config/frameworkTransforms.properties文件,增加一行

 

[c-sharp]  view plain copy
  1. ofbizUrl=org.ofbiz.webapp.ftl.OfbizUrlTransform  
  2. ofbizContentUrl=org.ofbiz.webapp.ftl.OfbizContentTransform  
  3. ofbizCurrency=org.ofbiz.webapp.ftl.OfbizCurrencyTransform  
  4. ofbizAmount=org.ofbiz.webapp.ftl.OfbizAmountTransform  
  5. setRequestAttribute=org.ofbiz.webapp.ftl.SetRequestAttributeMethod  
  6. renderWrappedText=org.ofbiz.webapp.ftl.RenderWrappedTextTransform  
  7. #在这里增加  
  8. EncodeUrl=sample.EncodeURLMethod  

最后,你可以直接在Freemarker模版文件中这样使用:

${encodeURL("/news/showNews.sf?id=123456")}

 

(对于FrameMarker自定义标签,也可以采用如上步骤)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值