freemark自定义标签

1. [代码]实现自己的自定义 freemarker标签     跳至 [1] [2] [3] [4] [全屏预览]

?
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
package com.f139.frame.freemarker.directive;
 
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
 
import org.nutz.dao.Dao;
 
import com.f139.frame.pojo.factory.Label;
 
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
 
public class LabelDirective implements TemplateDirectiveModel {
 
     private Dao dao;
 
     public LabelDirective(Dao dao) {
         this .dao = dao;
     }
 
     @Override
     @SuppressWarnings ( "unchecked" )
     public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
         
         for (TemplateModel templateModel : loopVars) {
             templateModel.toString();
         }
         
         // 真正开始处理输出内容
         Writer out = env.getOut();
 
         if (body != null ) {
             out.write(getContent(params));
             body.render(out);
         }
     }
 
     private String getContent(Map<?,?> params) {
         Integer id = Integer.parseInt(params.get( "id" ).toString());
         if ( null != id) {
             Label label = this .dao.fetch(Label. class , id);
             return label.getLabelContent();
         }
         return null ;
     }
}

2. [代码]将自定义freemarker 标签添加到ExtendServlet     

?
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
package com.f139.frame.servlet;
 
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.json.JsonLoader;
 
import com.f139.frame.tag.RepeatDirective;
 
import freemarker.ext.jsp.TaglibFactory;
import freemarker.ext.servlet.FreemarkerServlet;
import freemarker.template.Configuration;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
 
 
public class ExtendServlet extends FreemarkerServlet {
 
     /**
      *
      */
     private static final long serialVersionUID = 1L;
 
     private Dao dao;
 
     public ExtendServlet() {
         Ioc ioc = new NutIoc( new JsonLoader( "com/f139/frame/ioc" ));
         this .dao = ioc.get(NutDao. class , "dao" );
     }
 
     @Override
     protected Configuration createConfiguration() {
         //添加自定义标签
         Configuration cfg = super .createConfiguration();
         cfg.setSharedVariable( "label" , new LabelDirective(dao));
         return cfg;
     }
 
}
 
ExtendServlet继承FreemarkerServlet

3. [代码]在web.xml添加相应配置     

?
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
<servlet>
         <servlet-name>freemarker</servlet-name>
         <servlet- class >com.f139.frame.servlet.ExtendServlet</servlet- class >
         <init-param>
             <param-name>TemplatePath</param-name>
             <param-value>/</param-value>
         </init-param>
         <init-param>
             <param-name>NoCache</param-name>
             <param-value> true </param-value>
         </init-param>
         <init-param>
             <param-name>ContentType</param-name>
             <param-value>text/html;charset=UTF- 8 </param-value>
         </init-param>
         <init-param>
             <param-name>template_update_delay</param-name>
             <param-value> 0 </param-value>
         </init-param>
         <init-param>
             <param-name>default_encoding</param-name>
             <param-value>UTF- 8 </param-value>
         </init-param>
         <init-param>
             <param-name>number_format</param-name>
             <param-value> 0 .##########</param-value>
         </init-param>
         <load-on-startup> 1 </load-on-startup>
     </servlet>

4. [代码]使用自己的标签     

1
2
3
< @label id= 21 >
 
</ @label >


http://www.oschina.net/code/snippet_114990_4438


?

猜你喜欢

转载自blog.csdn.net/wwlinhappy/article/details/50420326
今日推荐