spring+freemarker自定义标签开发

 

手上的项目现在要支持多平台,多个平台的显示内容大部分相同,但是页面的部分存在区别,为了便于维护,自己开发了一个freemarker的自定义标签check,通过check标签,自动检查<@check></@check>之间的内容是显示。

第一步:把页面上不同的平台存在差异的区别起个名字name标识,在zk中配置,每个平台要显示的区域名称的列表,配置示例如下:

  {
    "azure": [
        "config.set" 
        ,"config.domain"
    ], 
    "cloudscape": [
        "config.set"
        ,"config.domain"
        , "config.rule"
    ]
}

 cloudscape平台显示config.set,config.domain,config.rule三个区域,azure显示config.set,config.domain两个区域

2.开发自定义标签:

    重点是: implements freemarker.template.TemplateDirectiveModel这个接口

 

import java.io.IOException;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.sohu.sce.console.services.ZookeeperService;
import com.sohu.sce.console.utils.consts.Constants;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

public class MyViewDirective implements TemplateDirectiveModel{

	@Autowired
	ZookeeperService zkService;
	
	private static JSONObject viewConfig ;
	
	@Override
	public void execute(Environment env, Map params, TemplateModel[] model,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		if(viewConfig== null){
			synchronized (MyViewDirective.class) {
				if(viewConfig == null){
					String config = zkService.getData("/conf/modules/console/view.config");
					viewConfig = JSONObject.fromObject(config);
				}
			}
		}
 
		
		String name = getRequiredParam(params, "name");
		String platfom = getParam(params, Constants.PLATFORM.NAME, Constants.PLATFORM.CLODUSCAPE);
		JSONArray viewList = viewConfig.getJSONArray(platfom);
		if(viewList != null && viewList.size() >0){
			if(viewList.contains(name)){
				body.render(env.getOut());
			}
		}
		
	}
	
	static String getRequiredParam(Map params,String key) throws TemplateException {
		Object value = params.get(key);
		if(value == null || StringUtils.isEmpty(value.toString())) {
			throw new TemplateModelException("not found required parameter:"+key+" for directive");
		}
		return value.toString();
	}
	
	static String getParam(Map params,String key,String defaultValue) throws TemplateException {
		Object value = params.get(key);
		return value == null ? defaultValue : value.toString();
	}

}
 3.在spring的配置文件增加配置:
  
<entry key="check">
  <bean class="com.sohu.sce.console.actions.freemarker.MyViewDirective" />
</entry>
 完成配置如下:
<bean id="freemarkerConfigurer"
     class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/" />
		<property name="defaultEncoding" value="UTF-8" />
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">10</prop>
				<prop key="locale">zh_CN</prop>
				<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
				<prop key="date_format">yyyy-MM-dd</prop>
				<prop key="number_format">#.##</prop>
				<prop key="auto_import">/ftl/common/macros.ftl as html</prop>
				<prop key="tag_syntax">auto_detect</prop>
			</props>
		</property>
		<property name="freemarkerVariables">
  		  <map>
		    <entry key="block">
		 	<bean class="cn.org.rapid_framework.freemarker.directive.BlockDirective" />
				</entry>

				<entry key="override">
					<bean
						class="cn.org.rapid_framework.freemarker.directive.OverrideDirective" />
				</entry>

				<entry key="extends">
					<bean
						class="cn.org.rapid_framework.freemarker.directive.ExtendsDirective" />
				</entry>

				<entry key="super">
					<bean class="cn.org.rapid_framework.freemarker.directive.SuperDirective" />
				</entry>
				<entry key="super">
					<bean class="cn.org.rapid_framework.freemarker.directive.SuperDirective" />
				</entry>
				<entry key="check">
					<bean class="com.sohu.sce.console.actions.freemarker.MyViewDirective" />
				</entry>
			</map>
		</property>
</bean>
 4.在页面上把需要做检查的区域用check标签包起来:
<@check name="config.rule" paltform="azure">
               区域的原html内容
</@check>
 经过以上四步,自定义的标签就可以使用了。
由于我的每一个平台的线上内容是固定的,如果你用自定义标签来做权限控制【这个场景使用的还是比较多的】,就不能像我这样配置了,可能需要根据抽象出一个角色出来,在角色中配置权限,用户再关联到角色上,然后在自定义标签的实现里面进行动态的控制了,本文重点是介绍如何在spring+freemarker框架下做freemarker的自定义标签的开发,你可以根据自己的页面变更你的实现逻辑即可。

  

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值