1、实现自定义FreemarkerManager
- import freemarker.template.Configuration;
- import freemarker.template.TemplateDirectiveModel;
- import freemarker.template.TemplateException;
- import org.apache.struts2.views.freemarker.FreemarkerManager;
- import org.springframework.context.ApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- import javax.servlet.ServletContext;
- import java.util.Map;
- /**
- * @desc: 增加自定义业务标签,此list中的string必须继承于TemplateDirectiveModel,并且配置在SPRING上下文中
- */
- public class CustomFreemarkerManager extends FreemarkerManager {
- protected Configuration createConfiguration(ServletContext servletContext) throws TemplateException {
- Configuration cfg = super.createConfiguration(servletContext);
- ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
- Map<String,Object> beans=(Map<String,Object>)ctx.getBeansOfType(TemplateDirectiveModel.class);
- for (String key : beans.keySet()) {
- Object bean = beans.get(key);
- if (bean != null && bean instanceof TemplateDirectiveModel)
- cfg.setSharedVariable(key, bean);
- }
- return cfg;
- }
- }
2、在struts.properties中配置你的自定义管理类
- struts.freemarker.manager.classname=com.kms.framework.core.view.support.KmsFreemarkerManager
3、自定义TemplateDirectiveModel
- import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import javax.servlet.http.HttpSession;
- import org.apache.struts2.ServletActionContext;
- import com.newyulong.bcp.product.domain.TypeInfo;
- import com.newyulong.bcp.product.service.ITypeService;
- import com.newyulong.bcp.user.vo.Loginer;
- import freemarker.core.Environment;
- import freemarker.template.TemplateDirectiveBody;
- import freemarker.template.TemplateDirectiveModel;
- import freemarker.template.TemplateException;
- import freemarker.template.TemplateModel;
- import freemarker.template.TemplateModelException;
- import freemarker.template.TemplateNumberModel;
- /**
- * @desc: 产品类别自定义标签
- */
- public class ProductTypeTemplateDirectiveModel implements TemplateDirectiveModel {
- //产品分类服务
- private ITypeService typeService;
- // 展示产品类别数目
- private static final String PARAM_COUNT = "count";
- // 默认
- private static final int DEAFAULT_COUNT = 5;
- public void execute(Environment environment, Map map,
- TemplateModel[] templateModels, TemplateDirectiveBody body)
- throws TemplateException, IOException {
- int count = 0, hot = 0;
- boolean countParamSet = false;
- // 获取传递进来的参数
- Iterator it = map.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry entry = (Entry) it.next();
- String paramName = entry.getKey().toString();
- TemplateModel paramValue = (TemplateModel) entry.getValue();
- if (paramName.equals(PARAM_COUNT)) {
- if (!(paramValue instanceof TemplateNumberModel)) {
- throw new TemplateModelException("The \"" + PARAM_COUNT
- + "\" parameter " + "must be a number.");
- }
- count = ((TemplateNumberModel) paramValue).getAsNumber()
- .intValue();
- countParamSet = true;
- if (count < 0) {
- throw new TemplateModelException("The \"" + PARAM_COUNT
- + "\" parameter " + "can't be negative.");
- }
- } else {
- throw new TemplateModelException("Unsupported parameter: "
- + paramName);
- }
- }
- // 参数为空时候设置默认值
- if (!countParamSet)
- count = DEAFAULT_COUNT;
- Map<String, Object> maps = getTypeList(String.valueOf(count));
- if (body != null) {
- environment.setVariable("typelist",
- DEFAULT_WRAPPER.wrap(maps.get("typeList")));
- // 真正开始处理输出内容
- body.render(environment.getOut());
- } else {
- throw new RuntimeException("missing body");
- }
- }
- /**
- *
- * @function 产品类别查询列表
- * @param count
- * @param hot
- * @return
- *
- */
- public Map<String, Object> getTypeList(String count) {
- Map<String, Object> maps = new HashMap<String, Object>();
- // 1.获取产品列表
- List<TypeInfo> list = typeService.getPreTypeList(count);
- maps.put("typeList", list);
- return maps;
- }
- // 获得用户信息
- private Loginer getLoginer() {
- HttpSession session = ServletActionContext.getRequest().getSession();
- return (Loginer) session.getAttribute(Loginer.USER_IN_SESSION);
- }
- public void setTypeService(ITypeService typeService) {
- this.typeService = typeService;
- }
4、在spring上下文中配置
- <!--产品类别标签-->
- <bean id="ptype" class="com.newyulong.bcp.web.tag.ProductTypeTemplateDirectiveModel"></bean>
5、在页面中使用你的标签吧
- <div class="gen">
- <h2 class="gen_tit">产品分类</h2>
- <ul>
- <!--产品分类-->
- <@ptype>
- <#list list as entity>
- <li class="bto">?
- <a href="${ctx}/web/productt.do?type=${entity.id}">
- <#if entity.title?length gt 12 >
- ${entity.title[0..11]}...
- <#else>
- ${entity.title}
- </#if>
- </a>
- </li>
- </#list>
- </@ptype>
- </ul>
- </div>