- 浏览: 742772 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
lgh1992314:
a offset: 26b offset: 24c offse ...
java jvm字节占用空间分析 -
ls0609:
语音实现在线听书http://blog.csdn.net/ls ...
Android 语音输入API使用 -
wangli61289:
http://viralpatel-net-tutorials ...
Android 语音输入API使用 -
zxjlwt:
学习了素人派http://surenpi.com
velocity宏加载顺序 -
tt5753:
谢啦........
Lucene的IndexWriter初始化时的LockObtainFailedException的解决方法
今天使用velocity在java后台渲染一个map<Long,String>对象到vm模板上,通过get(111)获取不到map的对象的值分析。
这是velocity1.6.4的处理,以后版本是不是解决了这个问题可以再查,个人认为在vm文件支持跟java一样加个L表示long之类的处理应该不难。
具体代码如下:
后台java生成设置代码
Map<Long,String> map = new HashMap<Long,String>(); map.put(101L,"aaa"); map.put(102L,"bbb"); context.put("longMap",map); Map<Integer,String> intMap = new HashMap<Integer,String>(); intMap.put(101,"aaa"); intMap.put(102,"bbb"); context.put("intMap",intMap);
vm文件输出代码:
#foreach($item in $longMap.entrySet()) $item.key $item.value #end $longMap.get(101) #foreach($item in $intMap.entrySet()) $item.key $item.value #end $intMap.get(101)
打印输出如下:
102 bbb 101 aaa $longMap.get(101) 102 bbb 101 aaa aaa
可以看到使用Map<Long,String>的map类型,通过$longMap.get(101)获取不到值,而使用Map<Integer,String>类型的map,通过$intMap.get(101)能够正常取到值aaa,而通过entrySet来遍历都没有问题,为什么会这样呢?
那就查看velocity的源码,查看这块的处理了,
最终是在velocity中通过#set($a=101)或者直接使用$intMap.get(101)是,会把101生成存储到一个变量中,而变量的类型当然根据这个值来了,不过101当然处理成整数型,而'aaa'则处理成字符串,101.1处理成浮点型。
但是整数型有short,int,long,BigInteger,浮点型有float,double,BigDecimal等,那么怎么处理值的类型了。
请看velocity的下面两个类就知道了:
ASTIntegerLiteral.java处理整数型的转换的:
public class ASTIntegerLiteral extends SimpleNode { // This may be of type Integer, Long or BigInteger private Number value = null; /** * @param id */ public ASTIntegerLiteral(int id) { super(id); } /** * @param p * @param id */ public ASTIntegerLiteral(Parser p, int id) { super(p, id); } /** * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.ParserVisitor, java.lang.Object) */ public Object jjtAccept(ParserVisitor visitor, Object data) { return visitor.visit(this, data); } /** * @see org.apache.velocity.runtime.parser.node.SimpleNode#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object) */ public Object init( InternalContextAdapter context, Object data) throws TemplateInitException { /* * init the tree correctly */ super.init( context, data ); /** * Determine the size of the item and make it an Integer, Long, or BigInteger as appropriate. */ String str = getFirstToken().image; try { value = new Integer( str ); } catch ( NumberFormatException E1 ) { try { value = new Long( str ); } catch ( NumberFormatException E2 ) { // if there's still an Exception it will propogate out value = new BigInteger( str ); } } return data; } /** * @see org.apache.velocity.runtime.parser.node.SimpleNode#value(org.apache.velocity.context.InternalContextAdapter) */ public Object value( InternalContextAdapter context) { return value; } }
请看init方法,会尝试先把整数转换成Integer类型,转换失败再尝试转换成Long,再转换失败再转换成BigInteger,所以101肯定先转换成Integer成功了。
而且velocity没有语法直接直接设置类型为Long型。
同样对于浮点型也一样:
ASTFloatingPointLiteral.java类
public class ASTFloatingPointLiteral extends SimpleNode { // This may be of type Double or BigDecimal private Number value = null; /** * @param id */ public ASTFloatingPointLiteral(int id) { super(id); } /** * @param p * @param id */ public ASTFloatingPointLiteral(Parser p, int id) { super(p, id); } /** * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.ParserVisitor, java.lang.Object) */ public Object jjtAccept(ParserVisitor visitor, Object data) { return visitor.visit(this, data); } /** * Initialization method - doesn't do much but do the object * creation. We only need to do it once. * @param context * @param data * @return The data object. * @throws TemplateInitException */ public Object init( InternalContextAdapter context, Object data) throws TemplateInitException { /* * init the tree correctly */ super.init( context, data ); /** * Determine the size of the item and make it a Double or BigDecimal as appropriate. */ String str = getFirstToken().image; try { value = new Double( str ); } catch ( NumberFormatException E1 ) { // if there's still an Exception it will propogate out value = new BigDecimal( str ); } return data; } /** * @see org.apache.velocity.runtime.parser.node.SimpleNode#value(org.apache.velocity.context.InternalContextAdapter) */ public Object value( InternalContextAdapter context) { return value; } }
先尝试转换成Float,失败再转换成Double,再失败再转换成BigDecimal。
velocity语法也没有设置为double型的设置。
所以就会出现上面那个case了,$map.get(101) map存的是long跟string的键值对,用int型取肯定取不到,因为long跟int的hashcode不一样,这个可以查看map的处理。
发表评论
-
对字符串进行验证之前先进行规范化
2013-09-17 23:18 13895对字符串进行验证之前先进行规范化 应用系统中经常对字 ... -
使用telnet连接到基于spring的应用上执行容器中的bean的任意方法
2013-08-08 09:17 1439使用telnet连接到基于spring的应用上执行容器中 ... -
jdk7和8的一些新特性介绍
2013-07-06 16:07 10077更多ppt内容请查看:htt ... -
java对于接口和抽象类的代理实现,不需要有具体实现类
2013-06-12 09:50 2916原文链接:http://www.javaarch.net/j ... -
Excel2007格式分析和XML解析
2013-06-07 09:56 10631在物料清单采购中,用到excel上传文件解析功能,不 ... -
Java EE 7中对WebSocket 1.0的支持
2013-06-05 09:27 3804原文链接:http://www.javaarch.n ... -
java QRCode生成示例
2013-06-05 09:26 1474原文链接:http://www.javaarch.n ... -
Java Web使用swfobject调用flex图表
2013-05-28 19:05 1084Java Web使用swfobject调用 ... -
spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置
2013-05-21 15:57 3295spring使用PropertyPlaceholderCon ... -
java国际化
2013-05-20 20:57 4442java国际化 本文来自:http://www.j ... -
RSS feeds with Java
2013-05-20 20:52 1189RSS feeds with Java 原文来自:htt ... -
使用ibatis将数据库从oracle迁移到mysql的几个修改点
2013-04-29 10:40 1642我们项目在公司的大战略下需要从oracle ... -
线上机器jvm dump分析脚本
2013-04-19 10:48 2870#!/bin/sh DUMP_PIDS=`p ... -
eclipse远程部署,静态文件实时同步插件
2013-04-06 20:18 5425eclipse 远程文件实时同步,eclipse远程 ... -
java价格处理的一个问题
2013-03-26 21:21 1803我们经常会处理一些价格,比如从运营上传的文件中将某 ... -
java 服务降级开关设计思路
2013-03-23 16:35 3731java 服务屏蔽开关系统,可以手工降级服务,关闭服 ... -
poi解析excel内存溢出
2013-03-20 22:21 6352真是悲剧啊,一个破内部使用系统20多个人使用的后 ... -
简单web安全框架
2013-03-16 11:56 1506web安全框架,主要用servlet filter方 ... -
基于servlet的简单的页面缓存框架
2013-03-11 19:27 1186基于servlet的页面级缓存框架的基本用法: 代码参考: ... -
Eclipse使用过程中出现java.lang.NoClassDefFoundError的解决方案
2013-02-01 17:22 1482如果jdk,classpath设置正确,突然在eclipse ...
相关推荐
#set($name = "hello") 说明:velocity中变量是弱类型的。 当使用#set 指令时,括在双引号中的字面字符串将解析和重新解释,如下所示: #set($directoryRoot = "www" ) #set($templateName = "index.vm" ) #set($...
Velocity 文档中文版 Velocity 是一个基于 Java 的模板引擎(template engine),它允许任何人仅仅简单的使用模板语言(template language)来引用由 Java 代码定义的对象。 Velocity 的能力远不止 web 站点开发这...
注意:在 Velocity 中使用 $2.5 这样的货币标识是没有问题的,因为 Velocity 中的变量总是以一个大写或者小写的字母开始的。 2. 变量规范的写法: ${name} ,也可以写成:$name 。提倡用前面的写法。例如: 你...
Velocity模板适用于大量模板使用的场景,支持复杂的逻辑运算,包含 基本数据类型、变量赋值和函数等功能。Velocity.js 支持 Node.js 和浏览器环境。 Features 支持客户端和服务器端使用 语法是富逻辑的,构成门...
例如,在文档模板中,有一个条件语句#if(${ssq_type[0]} == '基本类型' ),如果ssq_type[0]的值等于'基本类型',则输出■,否则输出□。 在 Velocity 模板语言中,还有其他重要的组成部分,如#foreach()语句,这个...
Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎
NULL 博文链接:https://liuzidong.iteye.com/blog/1308261
以下是对velocity显示List与Map的方法进行了详细的介绍。需要的朋友可以过来参考下
Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity
问题的核心在于高版本的springboot已经删除了velocity支持的类 PS: 网上解决方法: 1.降低pring-context-support版本,但是强制降级影响其他代码运行因此发布一个不降低版的方法 2.依赖velocity-engine-core 但是...
Velocity教程
Velocity可用来从模板产生web 页面,SQL, PostScript以及其他输出。他也可用于一个独立的程序以产生源代码和报告,或者作为其它系统的一个集成组件。这个项目完成后,Velocity将为Turbine web 应用程序框架提供模板...
velocity 2.0 包需要的 jar 包 官方下载的,测试过了,可以运行 Demo 网上都有,关键几行代码: // ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // 按文件加载 // ve.init(); // Template t = ...
Velocity 和 FreeMarker区别 对于大部分的应用来说,使用 FreeMarker 比 Velocity 更简单,因为 Velocity 还必须编写一些自定义的
打开网址:http://www.web-tag.net/all_17.htm 就是velocity标签大全 、教程 或下载CHM格式帮助文档
本课程从velocity engine也就是velocity引擎开始, 先讲解velocity的基本使用以及基础语法 , 然后再讲解velocity 的进阶内容velocity Tools , 以及velocity作为web项目的视图改如何使用 , 每一部分都会有一个综合案例...
velocity 电子书velocity 电子书velocity 电子书
在使用velocity作为视图层时候,经常会碰到为空的判断,这里整理了velocity为空判断
关于velocity的学习资料,velocity教程和velocity用户手册
Velocity入门教程,语法,Velocity布局,Spring框架集成Velocity