SSH 项目 Freemarker自定义标签配置使用

1.spring 配置 Freemarker

     struts 对Freemarker封装有点深,很多Freemarker自定义配置需要再资源配置文件中配置


<!-- 配置freemarkerManager -->
<bean id="freemarkerManager" class="org.apache.struts2.views.freemarker.FreemarkerManager" />


classic_compatible=true 
whitespace_stripping=true 
template_update_delay=0 
locale=zh_CN 
default_encoding=utf-8 
url_escaping_charset=utf-8 
date_format=yyyy-MM-dd 
time_format=HH:mm:Ss 
datetime_format=yyyy-MM-dd 
number_format=\#0.\#\#\#\#\# #
template_exception_handler=ignore


2.Spring 定义初始自定义标签类

<bean id="xInitializing" class="net.shopxx.common.Initializing">
	</bean>



初始标签类 cms_article_list  cms_timeConvert
package net.shopxx.common;
import javax.annotation.Resource;
import javax.servlet.ServletContext;

import net.shopxx.constant.JdbcConstant;
import net.shopxx.tag.ArticleListDirective;
import net.shopxx.tag.TimeConvetDirective;

import org.apache.struts2.views.freemarker.FreemarkerManager;
import org.springframework.beans.factory.InitializingBean;

import freemarker.template.Configuration;



public class Initializing implements InitializingBean {
	

	
	@Resource
	private ServletContext servletContext;
	
	@Resource
	private FreemarkerManager freemarkerManager;

	@Override
	public void afterPropertiesSet() throws Exception {

		
		Configuration configuration = freemarkerManager.getConfiguration(servletContext);
		configuration.setSharedVariable("devModel", JdbcConstant.DEVMODEL);
		configuration.setSharedVariable("cms_article_list", new ArticleListDirective());
		configuration.setSharedVariable("cms_timeConvert", new TimeConvetDirective());

		
	}
}



3.编写自定义标签类 

cms_article_list  cms_timeConvert

package net.shopxx.tag;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public class TimeConvetDirective implements TemplateDirectiveModel {

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		Writer out = env.getOut();
		TemplateModel paramValue = (TemplateModel) params.get("title");
		//使用h1标签包装title值
		out.write("<h1>" + paramValue.toString() + "</h1>");
		//如果标签中有内容
		if (body != null) {
			body.render(env.getOut());
		} else {
			//如果标签中没有内容
			//throw new RuntimeException("标签内部至少要加一个空格");
		}
		
	}

}



cms_article_list  

package net.shopxx.tag;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public class ArticleListDirective implements TemplateDirectiveModel {

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		List<String> list = new ArrayList<String>();
		if (params.containsKey("parent_id") && params.get("parent_id") != null) {
			list.add("a");
			list.add("b");
			list.add("c");
		} else {
			list.add("1");
			list.add("2");
			list.add("3");
		}
		
		env.setVariable("article_list", DEFAULT_WRAPPER.wrap(list));
		body.render(env.getOut());

	}

}



4.页面使用自定义标签

<!--s 尾部-->	
		<#assign name="dfdsdfsdf">
		
		<@cms_timeConvert title="${name}"/>
	
		<@cms_article_list>
			<#if article_list??>
				<#list article_list as bean>
					${bean}
				</#list>
			<#else>
			   NOT DATA
			</#if>
		</@cms_article_list>
		
		<!--e 尾部-->



你可能感兴趣的:(SSH 项目 Freemarker自定义标签配置使用)