简体   繁体   English

速度上下文方法出错

[英]Error with velocity context method

i'm traying to put in velocity context one method: 我正在努力将速度上下文放入一种方法:

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


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

}

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

and getting error : 并得到错误:

Encountered "(" at line 175, column 20.

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

where is the problem ? 问题出在哪儿 ?

after looking at your code once more i think you want to put some kind of "magic link" into your context so you can simply call that method. 在再次查看您的代码之后,我认为您希望在您的上下文中添加某种“魔术链接”,以便您可以简单地调用该方法。 But the context only contains objects. 但是上下文只包含对象。

You can achieve what you want by putting your method into a utility class: 通过将方法放入实用程序类,您可以实现所需的目标:

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);
    }
}

then you can put an instance of your utility class into your context: 然后,您可以将实用程序类的实例放入上下文中:

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

and use it in your template: 并在您的模板中使用它:

$rounder.roundServiceTime($someValue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM