0

我正在尝试将一种方法放入速度上下文中:

ctx.put("round", roundServiceTime(serviceTimeRound));


public int roundServiceTime (int serviceTimeRound) {
    double sum = serviceTimeRound/60;
    this.serviceTimeRound = (int)Math.ceil((double)sum);
    return serviceTimeRound;

}

错误行:#set($val = $round(90))

并得到错误:

Encountered "(" at line 175, column 20.

期待以下之一: ... ... “-” ... “+” ... “*” ... “/” ... “%” ... “&&” ... “|| " ... "<" ... "<=" ... ">" ... ">=" ... "==" ... "!=" ... ...

问题出在哪里 ?

4

1 回答 1

2

再次查看您的代码后,我认为您想将某种“魔术链接”放入您的上下文中,以便您可以简单地调用该方法。但是上下文只包含对象。

您可以通过将方法放入实用程序类来实现您想要的:

public class Rounder {

    public static final Rounder INSTANCE = new Rounder(); 

    public int roundServiceTime (int serviceTimeRound) {
        double sum = serviceTimeRound / 60.0;
        return (int)Math.ceil(sum);
    }
}

然后您可以将实用程序类的实例放入您的上下文中:

ctx.put("rounder", Rounder.INSTANCE);

并在您的模板中使用它:

$rounder.roundServiceTime($someValue)
于 2013-05-27T09:24:57.980 回答