Struts2 Freemarker 自定义标签

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值