1:FreemarkerManager:
package com.fangtoon.appcenterui.framework.struts2;
import java.util.Map;
import java.util.Map.Entry;
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.TemplateException;
import freemarker.template.TemplateModel;
public class StrutsFreemarkerManager extends FreemarkerManager {
@Override
protected Configuration createConfiguration(ServletContext servletContext) throws TemplateException {
Configuration configuration = super.createConfiguration(servletContext);
configuration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX); // 设置标签类型([]、<>),[]这种标记解析要快些
ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); // 取出上下文
Map<String, TemplateModel> beans = applicationContext.getBeansOfType(TemplateModel.class); // SpringContextHolder.getApplicationContext().getBeansOfType(TemplateDirectiveModel.class); // 获取实现TemplateDirectiveModel的bean
for (Entry<String, TemplateModel> entry : beans.entrySet()) {
if (entry.getValue() != null && entry.getValue() instanceof TemplateModel) {
configuration.setSharedVariable(entry.getKey(), entry.getValue());
}
}
return configuration;
}
}
2:struts.properties
struts.freemarker.manager.classname=com.fangtoon.appcenterui.framework.struts2.StrutsFreemarkerManager
3:具体的自定义标签实现:
package com.fangtoon.appcenterui.framework.struts2.tag.method;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;
@Component("dictItemName")
public class DictItemNameTemplate implements TemplateMethodModel {
private Logger logger = LoggerFactory.getLogger(getClass());
@Override
@SuppressWarnings({ "rawtypes" })
public Object exec(List args) throws TemplateModelException {
String dictCode = (String) args.get(0);
String dictItemCode = (String) args.get(1);
return dictCode + " - " + dictItemCode;
}
}
4:页面使用:
${dictItemName("APP_AUDIT_STATE","SUB_DICT_CODE")}
输出:
APP_AUDIT_STATE - SUB_DICT_CODE
5: 自定义有方法体标签:
package com.fangtoon.appcenterui.framework.struts2.tag;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import freemarker.core.Environment;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
@Component("forloop")
public class LoopsTemplateDirectiveModel implements TemplateDirectiveModel {
@SuppressWarnings("rawtypes")
public void execute(Environment environment, Map map, TemplateModel[] atemplatemodel, TemplateDirectiveBody templatedirectivebody) throws TemplateException, IOException {
environment.setVariable("loops", ObjectWrapper.DEFAULT_WRAPPER.wrap(getLoops()));
templatedirectivebody.render(environment.getOut());
}
private List<String> getLoops() {
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three")
return list;
}
}
页面使用:
[@forloop]
[#list loops as loop]
${loop}
[/#list]
[/@forloop]
输出: one two three