springmvc整合freemarker怎么弄

问题描述

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

时间: 2024-08-29 09:32:58

springmvc整合freemarker怎么弄的相关文章

pringmvc-求教,SpringMVC整合hessian,客户端找不到服务端的接口类!求配置代码活解决方案!谢谢!

问题描述 求教,SpringMVC整合hessian,客户端找不到服务端的接口类!求配置代码活解决方案!谢谢! 求教,SpringMVC整合hessian,客户端找不到服务端的接口类!求配置代码活解决方案!谢谢!

Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(转)

 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使前端应用能更快速和稳定的响应.   第一:介绍Dubbo背景              大规模服务化之前,应用可能只是通过RMI或Hessian等工具,简单的暴露和引用远程服务,通过配置服务的URL地址进行调用,通过F5等硬件进行负载均衡. (1) 当服务越来越多时,服务URL配置管理变得非常困难

Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(转)

互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使前端应用能更快速和稳定的响应.   第一:介绍Dubbo背景              大规模服务化之前,应用可能只是通过RMI或Hessian等工具,简单的暴露和引用远程服务,通过配置服务的URL地址进行调用,通过F5等硬件进行负载均衡. (1) 当服务越来越多时,服务URL配置管理变得非常困难,

SpringMVC整合hessian出现异常

问题描述 SpringMVC整合hessian出现异常 严重: Servlet.service() for servlet spring threw exception java.net.HttpRetryException: cannot retry due to redirection, in streaming mode at sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:

mybatis-mybaitis springmvc整合空指针异常

问题描述 mybaitis springmvc整合空指针异常 这是我的web.xml <?xml version="1.0" encoding="UTF-8"?> xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation=&quo

SpringMVC整合mybatis实例代码_java

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis . 一.逆向工程生成基础信息 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis G

springmvc整合jasperreport,中文不显示

问题描述 springmvc整合jasperreport,中文不显示 我用sping mvc整合jasperreport做报表输出的部分,预览时中文显示不出来,itext,itextasian我都加过了,这是什么原因呢

求大神:springmvc整合hibernate在web.xml都需要注入什么啊

问题描述 求大神:springmvc整合hibernate在web.xml都需要注入什么啊 contextConfigLocation/WEB-INF/spring/root-context.xml <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.Conte

springMvc 整合oracle数据库出现的问题

问题描述 用springmvc整合oracle数据库的时候报错如下org.springframework.beans.factory.NoSuchBeanDefinitionException:Nomatchingbeanoftype[com.dao.TestRespository]foundfordependency:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotati