最近在研究jeecms,虽然已经有很多标签了,但是由于需要,需要增加新的标签。
比如增加一个标签,可以根据会员组id查出该组下所有的会员。
其中CmsUser CmsUserDao 及CmsUserMng原系统已有,查询会员方法也有,因此只需编写一个FreeMarker标签类就行了。此标签类需要实现TemplateDirectiveModel接口。
public class CmsMemberListDirective implements TemplateDirectiveModel {
/**
* 模板名称
*/
public static final String TPL_NAME = "member_list";
/**
* @Fields GROUP_ID : 输入参数,会员组ID
*/
private static final String GROUP_ID = "groupId";
/**
* @Fields DISABLED : 输入参数,是否禁用
*/
private static final String DISABLED = "disabled";
public void execute(Environment env, Map params, TemplateModel[] arg2,
TemplateDirectiveBody body) throws TemplateException, IOException {
CmsSite site = FrontUtils.getSite(env);
Integer groupId = getId(params);
Boolean disabled = DirectiveUtils.getBool(DISABLED, params);
List list = cmsUserMng.getList(null, null, null, groupId, disabled, null, null);
Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(
params);
paramWrap.put(OUT_LIST, DEFAULT_WRAPPER.wrap(list));
Map<String, TemplateModel> origMap = DirectiveUtils
.addParamsToVariable(env, paramWrap);
InvokeType type = DirectiveUtils.getInvokeType(params);
String listStyle = DirectiveUtils.getString(PARAM_STYLE_LIST, params);
if (InvokeType.sysDefined == type) {
if (StringUtils.isBlank(listStyle)) {
throw new ParamsRequiredException(PARAM_STYLE_LIST);
}
env.include(TPL_STYLE_LIST + listStyle + TPL_SUFFIX, UTF8, true);
} else if (InvokeType.userDefined == type) {
if (StringUtils.isBlank(listStyle)) {
throw new ParamsRequiredException(PARAM_STYLE_LIST);
}
FrontUtils.includeTpl(TPL_STYLE_LIST, site, env);
} else if (InvokeType.custom == type) {
FrontUtils.includeTpl(TPL_NAME, site, params, env);
} else if (InvokeType.body == type) {
body.render(env.getOut());
} else {
throw new RuntimeException("invoke type not handled: " + type);
}
DirectiveUtils.removeParamsFromVariable(env, paramWrap, origMap);
}
private Integer getId(Map<String, TemplateModel> params)
throws TemplateException {
Integer id = DirectiveUtils.getInt(GROUP_ID, params);
if (id != null) {
return id;
} else {
throw new ParamsRequiredException(GROUP_ID);
}
}
@Autowired
private CmsUserMng cmsUserMng;
}
写完后需要在在jeecms-context.xml文件中加入CmsChannelStatisticsDirective标签类的bean注入代码。
<bean id="cms_member_list" class="com.jeecms.cms.action.directive.CmsMemberListDirective"/>
接着还要在在jeecms-servlet-front.xml文件中配置:
<entry key="cms_member_list" value-ref="cms_member_list"/>
<entry key="cms_member_page" value-ref="cms_member_page"/>
配完后重启项目,可以在栏目模板中使用标签了。
</pre>
[@cms_member_list groupId = '1']
[#list tag_list as a]
<li>${a.username}</li>
[/#list]
[/@cms_member_list]
<pre>