`

spring mvc 中使用velocity 做邮件模板设置日期格式的方法

阅读更多
首先需要在spring的配置文件中配置
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
      <property name="velocityProperties">
         <value>
          resource.loader=class
          class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
         </value>
      </property>
   </bean>

这样配置之后就可以解析模板文件了,但是无法格式化日期,数字,要做格式化需要把DateTool和NumberTool的实例放到Model中如下代码
/**
	 * 生成模板内容
	 * @param vmTemplatePath 模板资源文件的路径
	 * @param modelMap 模板数据
	 * @return
	 */
	private String generateContentImpl(String vmTemplatePath,Map<String,Object> modelMap) {		
		if(modelMap.containsKey("dateTool")){
			throw new ArgumentException("modelMap cannot contains dateTool ,dateTool is used to format date function");
		}
		
		if(modelMap.containsKey("numberTool")){
			throw new ArgumentException("modelMap cannot contains numberTool ,numberTool is used to format number function");
		}
		
		
		DateTool dateTool = new DateTool();
		modelMap.put("dateTool",dateTool);
		NumberTool numberTool = new NumberTool();
		modelMap.put("numberTool", numberTool);
		
		String text = VelocityEngineUtils.mergeTemplateIntoString(
				velocityEngine, vmTemplatePath, "UTF-8",modelMap);
		
		return text;
	}

当然需要引用velocity-tools jar包。
分享到:
评论

相关推荐

    spring mvc 3.2 参考文档

    在Spring Web MVC 中,您可以使用任何对象作为命令对象(command orject)或表单对象 ;您不需要实现框架特定接口或基类。Spring的数据绑定是高度灵活的: 例如,它把类型不匹配当做验证错误,这样就可以算作应用程序...

    velocity入门使用教程

    本人写的velocity教程,包括模板引擎基本使用方法,velocity语法,servlet+velocity例子,spring mvc+velocity例子

    cassandratemplate:Spring Mvc + Spring Data + Mysql + Cassandra Web 模板

    还使用了 Spring 数据和 spring mvc。 Velocity 被选择并配置为模板引擎。 为了运行程序 cassandra.properties 和 mysql.properties 文件应该根据用户的偏好设置进行配置。 至少 mysql 和 cassandra 应该在本地机器...

    spring jar 包详解

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...

    spring mvc + easyui

    J2ee开源模板后台是基于 springmvc+mybatis+easyui+velocity 技术实现的一套模板后台,把一些通用的功能模板化,可以在线打包生成整站源码包括 jsp页面,为开发人员节省时间 ,生成的功能有增删改查,查询包括按列...

    spring in action英文版

     4.2 在Spring中使用JDBC  4.2.1 JDBC代码的问题  4.2.2 使用JdbcTemplate  4.2.3 把操作创建成对象  4.2.4 自增键  4.3 介绍Spring的ORM框架支持  4.4 用Spring整合Hibernate  4.4.1 ...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用都要...

    Spring in Action(第二版 中文高清版).part2

    16.4.3 在JSF页面中使用Spring Bean 16.4.4 在JSF中暴露应用程序环境 16.5 Spring中带有DWR的支持Ajax的应用程序 16.5.1 直接Web远程控制 16.5.2 访问Spring管理的Bean DWR 16.6 小结 附录A 装配Spring A.1 ...

    Spring in Action(第二版 中文高清版).part1

    16.4.3 在JSF页面中使用Spring Bean 16.4.4 在JSF中暴露应用程序环境 16.5 Spring中带有DWR的支持Ajax的应用程序 16.5.1 直接Web远程控制 16.5.2 访问Spring管理的Bean DWR 16.6 小结 附录A 装配Spring A.1 ...

    最新最全的spring开发包

    这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用都要...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring4.1核心包

    UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类, 6. spring-core-4.1.1.RELEASE.jar spring核心包 7. spring-expression-4.1.1.RELEASE.jar spring表达语言 SpEL以"#{...}...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Thymeleaf3模版开发中文手册.zip

    类似JSP,Velocity,FreeMaker等, 它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比, Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个...

    thymeleaf pdf

    hymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP,或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好...

    springboot参考指南

    在Spring环境中使用YAML暴露属性 iii. 23.6.3. Multi-profile YAML文档 iv. 23.6.4. YAML缺点 vii. 23.7. 类型安全的配置属性 i. 23.7.1. 第三方配置 ii. 23.7.2. 松散的绑定(Relaxed binding) iii. 23.7.3. @...

    基于SpringBoot的代码生成器的设计和实现.doc

    本系统使用模板引擎Apache Velocity来实现从模型到代码的渲染,使用MySQL数据库来储存模型和模板信息,网页前端使用Vue框架和Ajax技术进行设计。本文对该设计进行了初步的实现,并对代码生成功能进行测试,生成的...

Global site tag (gtag.js) - Google Analytics