问题描述
springmvc整合freemarker整合流程和原理是什么?最好能有个demo! 问题补充:spirngmvc中freemarker自定义标签的使用原理
解决方案
一、 用macro实现自定义指令,例如:自定义指令可以使用macro指令来定义。 <#macro greet person> <font size="+2">Hello ${person}!</font> </#macro> macro指令自身不打印任何内容,它只是用来创建宏变量,所以就会有一个名为greet的变量。使用这个宏: <@greet person="Fred"/>会打印出: <font size="+2">Hello Fred!</font>二、用java代码标签实现自定义指令:可以使用TemplateDirectiveModel接口在Java代码中实现自定义指令。简单示例如下:1、实现TemplateDirectiveModel接口。 public class UpperDirective implements TemplateDirectiveModel { public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { if (!params.isEmpty()) { throw new TemplateModelException( "parameters 此处没有值!"); } if (loopVars.length != 0) { throw new TemplateModelException( " variables 此处没有值!"); } if (body != null) { //执行nested body 与FTL中 <#nested> 类似。 body.render(new UpperCaseFilterWriter(env.getOut())); } else { throw new RuntimeException("missing body"); } } private static class UpperCaseFilterWriter extends Writer { private final Writer out; UpperCaseFilterWriter (Writer out) { this.out = out; } public void write(char[] cbuf, int off, int len) throws IOException { char[] transformedCbuf = new char[len]; for (int i = 0; i < len; i++) { transformedCbuf[i] = Character.toUpperCase(cbuf[i + off]); } out.write(transformedCbuf); } public void flush() throws IOException { out.flush(); } public void close() throws IOException { out.close(); } } } 说明:<#nested>指令执行位于开始和结束标记指令之间的模板代码段。2、注入FreeMarkerConfigurer的freemarkerVariables中。 例如:在jeecms-servlet-front.xml <entry key="upper" value-ref="upper"/> <bean id="upper" class="com.example.UpperDirective" /> 说明: FreeMarkerConfigurer. 、setFreemarkerVariables(Map<String,Object> variables) 底层调用了FreeMarker的Configuration.setAllSharedVariables()方法。 因为更好的实践是将常用的指令作为共享变量放到Configuration中。3、调用自定义指令: [@upper] bar [#list ["red", "green", "blue"] as color] ${color} [/#list] baaz [/@upper]4、显示输出结果: BAR RED GREEN BLUE BAAZ
解决方案二:
freemarker的xml<?xml version="1.0" encoding="UTF-8"?><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:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache"xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!-- freemarker的配置 --><bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="templateLoaderPath" value="/WEB-INF/freemarker/" /><property name="defaultEncoding" value="utf-8" /><property name="freemarkerSettings"><props><prop key="template_update_delay">10</prop><prop key="locale">zh_CN</prop><prop key="datetime_format">yyyy-MM-dd</prop><prop key="date_format">yyyy-MM-dd</prop><prop key="number_format">#.##</prop></props></property></bean><!-- FreeMarker视图解析 。在这里配置后缀名ftl和视图解析器。。 --><bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property><property name="suffix" value=".ftl" /><property name="contentType" value="text/html;charset=utf-8" /><property name="exposeRequestAttributes" value="true" /><property name="exposeSessionAttributes" value="true" /><property name="exposeSpringMacroHelpers" value="true" /></bean></beans>模板ftl${user.uId}${user.uName}${user.uPassword}<#list userList as u>${u.uId}${u.uName}${u.uPassword}</#list>/** * @author kingcs */@Controller@RequestMapping("/springFreemarkerController")public class SpringFreemarkerController {/** * */@RequestMapping(value = "/freemarker", method = RequestMethod.GET)public String restList(Model model) {User user = new User();user.setuId(123);user.setuName("zhangsan");user.setuPassword("123456");user.setuBrithday(new Date());model.addAttribute("user", user);List<User> users = new ArrayList<User>();for(int i=0; i<10; i++) {User u = new User();u.setuId(i);u.setuName("zhangsan");u.setuPassword("123456");users.add(u);}model.addAttribute("userList", users);return "test";}}
解决方案三:
其实也没有什么原理之类的, 大致原理是:将页面中所需要的样式放入FreeMarker文件中,然后将页面所需要的数据动态绑定,并放入Map中,通过调用FreeMarker模板文件解析类process()方法完成静态页面的生成。了解了上面的原理,接下来我就一步 步带您实现FreeMarker生成静态页面。Demohttp://blog.csdn.net/daryl715/article/details/1657149