速度模板不替代方法 [英] Velocity templates not substituting methods

查看:27
本文介绍了速度模板不替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Velocity User's Guide 中,它看起来很简单在模板中使用方法.我试过了,但无法使它工作.有人能告诉我我做错了什么吗?

In the Velocity User's Guide it seems pretty straight forward to use methods in templates. I tried, but could not make it work. Can someone tell me what I did wrong?

谢谢.

这是我的代码

@Test
public void testVelocity() {
  Velocity.init();
  VelocityContext map = new VelocityContext();
  map.put("test", "Success");
  map.put("ok", "OK!");
  map.put("func", new Object() {public String test() {return "Yay!";}});
  map.put("func2", new Object() {public String test(String t) {return t+t;}});

  String template = "${func.test()} $test $func2.test($ok)";
  StringWriter writer = new StringWriter();
  Velocity.evaluate(map, writer, "", new StringReader(template));
  assertEquals("Yay! Success OK!OK!", writer.toString());
}

这是输出:

org.junit.ComparisonFailure: 
Expected :Yay! Success OK!OK!
Actual   :${func.test()} Success $func2.test($ok)

变量替换似乎工作正常,但不是方法调用.

Variable substitutions seems to work fine, but not method calls.

请帮忙.

推荐答案

问题是出于安全原因,Velocity 只允许调用公共类公共方法.匿名类不是公开的,因此调用被阻塞.

The problem is that for security reasons Velocity only allows calling public methods of public classes. An anonymous class is not public, thus the call is blocked.

如果你放置一个真实的对象,一个公共类的实例,它会起作用.

It would work if you put a real object, an instance of a public class.

如果您禁用安全 uberspector,它也可以工作,但这不是一个好主意,因为它会打开系统.

It would also work if you disable the secure uberspector, but that is not a good idea, since it opens up the system.

这篇关于速度模板不替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆