Java 速度+;智能化
我正在尝试开始使用ApacheVelocity模板引擎。我的IDE使用IntelliJ 以下是我在《Velocity开发指南》中的简单代码:Java 速度+;智能化,java,intellij-idea,velocity,Java,Intellij Idea,Velocity,我正在尝试开始使用ApacheVelocity模板引擎。我的IDE使用IntelliJ 以下是我在《Velocity开发指南》中的简单代码: public class velocityMain { public static void main(String[] args) { Properties props = new Properties(); props.setProperty("file.resource.loader.path", "~/I
public class velocityMain {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("file.resource.loader.path", "~/IdeaProjects/velocity_sample/web");
Velocity.init(props);
VelocityContext context = new VelocityContext();
context.put("foo", "foobar");
context.put("bar", "fooboo");
StringWriter writer = new StringWriter();
Velocity.mergeTemplate("template.vm", context, writer);
System.out.println(" template: " + writer);
String string = "We're using $bar $foo to render this.";
writer = new StringWriter();
Velocity.evaluate(context, writer, "mystring", string);
System.out.println(" string: " + writer);
}
}
以下是我的项目视图:
以下是template.vm内容:
<!DOCTYPE html>
<html>
<head>
<title>Velocity Test</title>
</head>
<body>
$foo
$bar
</body>
</html>
速度测试
$foo
$bar
当我在Tomcat8上部署应用程序时,template.vm
仅将变量$foo
和$bar
显示为纯文本。似乎来自velocityMain
类的代码甚至不会影响该页面
IntelliJ还表示方法Vetocity.mergeTemplate()
已被弃用
如果有人能帮我解决这个问题,我非常感谢。很抱歉,tomcat没有运行任何“main”,您应该澄清并告诉我们您正在做什么/试图做什么以及如何做。我自己使用VelocityViewServlet解决了这个问题。无论如何谢谢你!:)