templates - 在velocity模板中调用java方法

标签 templates velocity

我有一个类 CurrencyUtil,其中编写了一个方法 convertCurrency(String symbol, long value) 我想从速度模板调用此方法。我将此类的对象 map.put("formatter",currencyUtil); 放在模板中,我将标签用作 $formatter.convertCurrency($currency, $total) code> 但当渲染模板时,它不会打印结果。

这里我的问题是,java方法中的参数名称和模板中的参数名称应该相同吗?还是还有其他问题?

最佳答案

java方法和模板中的参数名称可以不同。您可以尝试使用以下示例来定位您的问题。

示例.java

package com.example.currency;

import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import java.io.*;

public class Example
{
    public Example(String templateFile)
    {
        try
        {

            Velocity.init("velocity.properties");

            VelocityContext context = new VelocityContext();
            CurrencyUtil cu = new CurrencyUtil();
            cu.setCurrencyRate("EUR", 1.25);
            context.put("formatter", cu); 

            Template template =  null;

            try
            {
                template = Velocity.getTemplate(templateFile);
            }
            catch( ResourceNotFoundException rnfe )
            {
                System.out.println("Example : error : cannot find template " + templateFile );
            }
            catch( ParseErrorException pee )
            {
                System.out.println("Example : Syntax error in template " + templateFile + ":" + pee );
            }


            BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(System.out));

            if ( template != null)
                template.merge(context, writer);


            writer.flush();
            writer.close();
        }
        catch( Exception e )
        {
            System.out.println(e);
        }
    }


    public static void main(String[] args)
    {
        Example t = new Example("example.vm");
    }
}

CurrencyUtil.java

package com.example.currency;

import java.util.Map;
import java.util.HashMap;

public class CurrencyUtil {
    private static Map<String, Double> rates = new HashMap<String, Double>();

    public double getCurrencyRate(String symbol){
        return rates.get(symbol);
    }
    public void setCurrencyRate(String symbol, double currencyRate){
        rates.put(symbol, currencyRate);
    }

    public double convertCurrency(String symbol, long value){
        return value * getCurrencyRate(symbol);
    }
}

example.vm

#set( $total = 10000000000)
#set( $currency = "EUR")
$formatter.convertCurrency($currency, $total) 

关于templates - 在velocity模板中调用java方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20786403/

相关文章:

javascript - 安全的 JavaScript 模板

c++ - io 用于 std::string 的非关联容器

c++ - 使用 "template"编写 C 或 C++ 库

spring-boot - 如何在同一个项目中同时拥有 Velocity 和 Thymeleaf,但 Controller 仅使用其中之一

java - 如何隐藏空的速度变量名称?

c++ - 使用 typeid 警告未使用的变量

c++ - 如何为模板类中定义的类定义 friend

node.js - 将变量值传递给 Node.js 中的速度模板

java - Apache 速度 : Which variables are available in templates?

Java机器人鼠标移动: setting speed?