读书人

velocity对set 类型的支持 地图 get输

发布时间: 2012-08-26 16:48:06 作者: rapoo

velocity对set 类型的支持 map get输出问题分析

今天使用velocity在java后台渲染一个map<Long,String>对象到vm模板上,通过get(111)获取不到map的对象的值分析。


这是velocity1.6.4的处理,以后版本是不是解决了这个问题可以再查,个人认为在vm文件支持跟java一样加个L表示long之类的处理应该不难。



具体代码如下:

后台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的处理。


读书人网 >软件架构设计

热点推荐