我正在努力将速度上下文放在一个方法中:
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.
期待以下之一: ... ... “ - ”...... “+”...... “*”...... “/”...... “%”...... “&安培;&安培;” ... “||” ... “<” ... “< =”...... “>” 中... “> =”...... “==”...... “!=”...... ...
问题出在哪里?
答案 0 :(得分: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)