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

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

复制代码
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.struts2.views.freemarker.FreemarkerManager;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import freemarker.template.Configuration;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
/**
 * 从spring取得所有的TemplateDirectiveModel类的对象,全部取出来,交给struts管理
 * @author Administrator
 *
 */
public class DirectiveFreemarkerManager extends FreemarkerManager{
    @Override
    protected Configuration createConfiguration(ServletContext servletContext)
            throws TemplateException {
        //调用你父类操作
        Configuration  configuration=super.createConfiguration(servletContext);
        //设定在页面使用的标签的类型 (([]、<>),[]这种标记解析要快些)      [] SQUARE_BRACKET_TAG_SYNTAX  ,  <>ANGLE_BRACKET_TAG_SYNTAX
        configuration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
        //取得spring所管理所有的对象 ClassPathXmlApplication,  WebAplicationContext
        ApplicationContext  appContext=WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        // 获取实现TemplateDirectiveModel的bean
        Map<String, TemplateDirectiveModel>  beans= appContext.getBeansOfType(TemplateDirectiveModel.class);
        //再把这些类,全部交给struts的freemarker来管理
        //Component("upper") public class UpperDirective implements TemplateDirectiveModel
        for(String key : beans.keySet()){
            Object obj=beans.get(key);  //TemplateDirectiveModel
            if(obj!=null && obj instanceof  TemplateDirectiveModel){
                configuration.setSharedVariable(key, obj);
            } 
        }
        return configuration;
    }
}
复制代码

在struts.xml中添加:

 <!-- 让struts来管理freemarker自定义标签类 struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager -->
    <constant name="struts.freemarker.manager.classname" value="com.xxxx.struts.DirectiveFreemarkerManager"></constant>

编写TemplateDirectiveModel,并交给Spring管理:

复制代码
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import org.springframework.stereotype.Component;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
 * 自定义指定,作用,把嵌套的内容中的小写全部转换为大写字母
 * 
 * @author Administrator
 *
 */
@Component("upper")
public class UpperDirective implements TemplateDirectiveModel {
    //evn  环境输出
    //params 参数
    //loopVars 循环
    //body   内容
    @Override
    public void execute(Environment env, Map params,
            TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        //如果有参数 应该不支持参数
        if(!params.isEmpty()){
            throw new TemplateModelException(
                    "这个指令不允许使用参数.");
        }
        if(loopVars.length!=0){
              throw new TemplateModelException(
                        "这个指令不允许使用循环参数.");
        }
         // 如果嵌套内容不为空的
        if(body!=null){
            //如果有内容,自己去重写输出流
            body.render( new UpperCaseFilterWriter(env.getOut()));
        
        }else{
            throw new RuntimeException("无内容异常");
        }
    }
    //内部类
    private static class UpperCaseFilterWriter extends Writer{
         private final Writer out;  //接收网页中的输出 流对象
         public UpperCaseFilterWriter(Writer out){
             this.out=out;
         }
          //cbuf 文字内容, off位移,len 文字长度
        @Override
        public void write(char[] cbuf, int off, int len) throws IOException {
            char[] transformedChar=new char[len];
            for(int i=0;i<len;i++){
                transformedChar[i]=Character.toUpperCase(cbuf[i+off]);
            }
            out.write(transformedChar); 
        }
        @Override
        public void flush() throws IOException {
          out.flush();
        }
        @Override
        public void close() throws IOException {
             out.close();
        } 
    }
}
复制代码

编写action,result—>字符串,type=”freemarker”:

<!-- 约定大于配置   /admin/Flinktype_list.action  --> 
   <package name="freemarkerPackage" namespace="/admin" extends="commonPackage" >
       <action name="*_*" class="{1}Action" method="{2}">
           <result name="{2}" type="freemarker">/admin/template/{1}/{2}.ftl</result> 
       </action>
   </package>

模板/template/upper.ftl:

复制代码
<!DOCTYPE html>
<html lang="ch-ZN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>freemarker自定义标签</title>
</head>
<body>  

    <@upper>
          bar
          <#-- All kind of FTL is allowed here -->
          <#list ["red", "green", "blue"] as color>
            ${color}
          </#list>
          baaz
     </@upper>
     
    
    <hr/>
    
    <@cms_linktype    limit="0,10" >
         请选择分类: <select>
               <#list  arrTypes as x>
                  <option value="${x.id}">${x.typeName}</option>
               </#list> 
         </select>
    </@cms_linktype>
</body>
复制代码

 

转载于:https://www.cnblogs.com/jpfss/p/8445129.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中使用 Freemarker定义标签,可以通过以下步骤实现: 1. 创建一个自定义标签类,继承 `freemarker.template.TemplateDirectiveModel` 接口,并实现其中的 `execute` 方法,该方法用于处理自定义标签的逻辑。 ```java @Component public class CustomTagDirective implements TemplateDirectiveModel { @Autowired private UserService userService; // 举例注入一个服务类 @Override public void execute(Environment environment, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { // 处理自定义标签逻辑,可以使用 environment、params、body 等参数 String userId = params.get("userId").toString(); User user = userService.getUserById(userId); environment.getOut().write(user.getName()); } } ``` 2. 在 Spring Boot 的配置文件中注册自定义标签类。 ```java @Configuration public class FreemarkerConfig { @Autowired private CustomTagDirective customTagDirective; @Bean public FreeMarkerConfigurer freeMarkerConfigurer() { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setTemplateLoaderPath("classpath:/templates"); Map<String, Object> variables = new HashMap<>(); variables.put("customTag", customTagDirective); configurer.setFreemarkerVariables(variables); return configurer; } } ``` 3. 在 Freemarker 模板中使用自定义标签。 ```html <#assign userId = "1" /> <@customTag userId=userId /> ``` 在以上代码中,我们首先通过 `<#assign>` 定义了一个变量 `userId`,然后通过 `<@customTag>` 调用自定义标签,并将 `userId` 作为参数传入。 这样就可以在 Spring Boot 中使用自定义Freemarker 标签了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值