Spring和FreeMarker整合(实现自定义标签)

首先,关于版本,本地测试的版本分别为spring-framework-2.5.6.SEC01,FreeMarker 。

首先,是web.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  
  <servlet>
  	<servlet-name>dispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcher</servlet-name>
  	<url-pattern>/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 没什么说的,都是规范。

其次,是dispatcher-servlet.xml的配置:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<context:annotation-config/>
	<bean id="sampleController" class="com.yuxinglab.controller.ExampleController"/>
	<!-- -视图解析器 -->
	<bean id="freemarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="contentType" value="text/html; charset=UTF-8"/>
		<property name="exposeRequestAttributes" value="false"/>
		<property name="exposeSessionAttributes" value="false"/>
		<property name="exposeSpringMacroHelpers" value="true"/>
		<property name="prefix" value="/WEB-INF/freemarker/"/>
		<property name="suffix" value=".ftl"/>
	</bean>
	<!-- FreeMarker配置 -->
	<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<!-- 变量配置 -->
		<property name="freemarkerVariables">
			<map>
				<entry key="content_list" value-ref="content_list"/>
			</map>
		</property>
		<property name="templateLoaderPath" value=""/>
		<property name="freemarkerSettings">
			<props>
				<prop key="tag_syntax">auto_detect</prop>
				<prop key="template_update_delay">5</prop>
				<prop key="defaultEncoding">UTF-8</prop>
				<prop key="url_escaping_charset">UTF-8</prop>
				<prop key="locale">zh_CN</prop>
				<prop key="boolean_format">true,false</prop>
				<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
				<prop key="date_format">yyyy-MM-dd</prop>
				<prop key="time_format">HH:mm:ss</prop>
				<prop key="number_format">0.######</prop>
				<prop key="whitespace_stripping">true</prop>
			</props>
		</property>
	</bean>
	<context:annotation-config/>
	<!-- 自定义标签 -->
	<bean id="content_list" class="com.yuxinglab.directive.TestDirective"/>
</beans>

 接下来是自定义标签的指向类:

package com.yuxinglab.directive;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;
import org.springframework.web.servlet.view.AbstractTemplateView;

import com.yuxinglab.domain.Content;

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

public class TestDirective implements TemplateDirectiveModel{

	public void execute(Environment env, Map params, 
			TemplateModel[] loopVars, TemplateDirectiveBody body) 
					throws TemplateException, IOException {
		ArrayList<Content> contentList=new ArrayList<Content>();
		Content con1=new Content("标题1", "此处无声胜有声1");
		Content con2=new Content("标题2", "此处无声胜有声2");
		Content con3=new Content("标题3", "此处无声胜有声3");
		Content con4=new Content("标题4", "此处无声胜有声4");
		contentList.add(con1);
		contentList.add(con2);
		contentList.add(con3);
		contentList.add(con4);
		Map<String, TemplateModel> paramWrap=new HashMap<String, TemplateModel>();
		paramWrap.put("tag_list", DEFAULT_WRAPPER.wrap(contentList));
		Set<Map.Entry<String, TemplateModel>> entrySet = paramWrap.entrySet();
		String key;
		TemplateModel value;
		for (Map.Entry<String, TemplateModel> entry : entrySet) {
			key = entry.getKey();
			value = env.getVariable(key);
			if (value != null) {
			}
			env.setVariable(key, entry.getValue());
		}
		body.render(env.getOut());
	}

}

 接下来是模板的配置:

<#if content_list?exists>
<table>
	<tr>标题</tr><tr>内容</tr>
<#list content_list as c>
	<tr>${c.title}</tr><tr>${c.content}</tr>
</#list>
</table>
</#if>

仅实现了功能,没有做任何的异常处理和封装。仅供参考。

猜你喜欢

转载自yuxinglab.iteye.com/blog/2023028
今日推荐