I want to generate controller layer code through velocity. I generate a mapping method:
@ResponseBody
@PostMapping(value = "\\${peacetrue.${moduleName}.urls.add}")
public ${ModuleName}VO add(${ModuleName}Add params) {
logger.info("add record[{}]", params);
return ${moduleName}Service.add(params);
}
and then I got exception:
{DomainName}Controller.java.vm[line 18, column 39]
Was expecting one of:
"[" ...
"|" ...
"}" ...
"}" ...
Then I wrote a unit test:
@Test
public void translate() {
Velocity.init();
Map<String, Object> singletonMap = Collections.singletonMap("foo", "bar");
StringWriter stringWriter = new StringWriter();
Velocity.evaluate(new VelocityContext(singletonMap), stringWriter, "log", "$foo");
Assert.assertEquals("bar", stringWriter.toString());
stringWriter = new StringWriter();
Velocity.evaluate(new VelocityContext(singletonMap), stringWriter, "log", "\\${com.${foo}.name}");
Assert.assertEquals("${com.bar.name}", stringWriter.toString());
}
So what should i do?
it can be achieved with implementation 'org.apache.velocity.tools:velocity-tools-generic:3.0'
@Test
public void translate() {
VelocityEngine engine = new VelocityEngine();
engine.init();
Map<String, Object> singletonMap = Collections.singletonMap("foo", "bar");
StringWriter stringWriter = new StringWriter();
Velocity.evaluate(new VelocityContext(singletonMap), stringWriter, "log", "$foo");
Assert.assertEquals("bar", stringWriter.toString());
stringWriter = new StringWriter();
ToolManager manager = new ToolManager(true, true);
manager.setVelocityEngine(engine);
manager.configure(getEasyFactoryConfiguration());
ToolContext context = manager.createContext();
context.put("foo","bar");
Velocity.evaluate(context, stringWriter, "log", "${esc.d}{com.${foo}.name}");
Assert.assertEquals("${com.bar.name}", stringWriter.toString());
}
private EasyFactoryConfiguration getEasyFactoryConfiguration() {
EasyFactoryConfiguration config = new EasyFactoryConfiguration();
config.toolbox("application").tool(EscapeTool.class);
return config;
}