手上的项目现在要支持多平台,多个平台的显示内容大部分相同,但是页面的部分存在区别,为了便于维护,自己开发了一个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的自定义标签的开发,你可以根据自己的页面变更你的实现逻辑即可。
本文详细介绍了如何在Spring+Freemarker框架下开发自定义标签,以实现在不同平台上显示部分内容的功能。包括配置Zookeeper用于存储不同平台的显示区域列表,实现自定义标签接口并进行参数解析,以及在页面上使用该标签进行内容检查。适用于需要根据不同平台展示不同区域内容的场景。
1591

被折叠的 条评论
为什么被折叠?



