有关在GAE(Java)上设置模板框架的任何教程? [英] Any tutorials on setting up a templating framework on GAE (Java)?

查看:126
本文介绍了有关在GAE(Java)上设置模板框架的任何教程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在Google App Engine上使用HTML格式的模板来格式化我们的电子邮件(使用Java),但在我的生活中,我无法找到一个体面的教程来说明如何设置。

b
$ b

我试着查看 StringTemplate ,但找不到任何示例从servlet的上下文中加载独立模板并用作格式化程序。



任何人都可以帮忙吗?我愿意接受任何建议,例如Velocity或FreeMarker,只要他们在GAE上运行。



谢谢



StringTemplate的文档可能非常混乱。最新版本(版本4)与以前版本( ST 而不是 StringTemplate STGroup 而不是 StringTemplateGroup 等)

它还具有外部依赖性'ANTLR'。 按照这些说明(链接包含链接),将'antlr'和'SimpleTemplate'罐放在服务器上的WEB-INF / lib目录中。



第2版引入了模板'groups',据我所知,它是从Web服务器上的文件加载模板所必需的。



所以为了使它工作,我必须定义一个模板组文件,其中包含以下内容,名为 emailTemplate.stg

  html_format( keyToReplace1,keyToReplace2):: =<<<< 
< html>
< body>
< div>
这是$ keyToReplace1 $
< br />
这是$ keyToReplace2 $
< / div>
< / body>
< / html>
>>

然后,我必须确保该文件可以通过我的代码通过相对URL访问。这很容易通过浏览器中的URL进行测试,例如:localhost:8888 / templates / emailTemplate.stg



然后,为了使用这个模板,我使用了以下代码:

  STGroup g = new STGroupFile(templates / emailTemplate.stg,'$','$') ; 
ST emailTemplate = g.getInstanceOf(html_format);
emailTemplate.add(keyToReplace1,第一个键值);
emailTemplate.add(keyToReplace2,第二个键值);
字符串结果= emailTemplate.render();


I'm trying to format our emails by using HTML-formatted templates on Google App Engine (using Java), but for the life of me I cannot find a decent tutorial for how to set this up.

I've tried looking at StringTemplate, but I cannot find any examples where a stand-alone template is loaded from a servlet's context and used as a formatter.

Can anyone help? I'm open to any suggestions, such as Velocity or FreeMarker, so long as they run on GAE.

Thanks

解决方案

Figured out how to do it.

The documentation for StringTemplate can be very confusing. The latest version (version 4) has different classes than previous versions (ST instead of StringTemplate, STGroup instead of StringTemplateGroup, etc)

It also has an external dependency on 'antlr'. Per these instructions (link contains links to jars needed), put the 'antlr' and 'SimpleTemplate' jars in the WEB-INF/lib directory on the server.

Version 2 introduced template 'groups', which as far as I can tell, are required for loading a template from a file on a web server.

So to get it working, I had to define a template group file, with the following content, named emailTemplate.stg

html_format(keyToReplace1, keyToReplace2) ::= <<
<html>
<body>
  <div>
    This is $keyToReplace1$
    <br/>
    This is $keyToReplace2$
  </div>
</body>
</html>
>>

I then had to ensure this file was accessible by my code via a relative URL. This is easily testable by going to the URL in the browser, such as at: localhost:8888/templates/emailTemplate.stg

Then, to use this template, I used the following code:

STGroup g = new STGroupFile("templates/emailTemplate.stg", '$', '$');
ST emailTemplate = g.getInstanceOf("html_format");
emailTemplate.add("keyToReplace1", "value for the first key");
emailTemplate.add("keyToReplace2", "value for the second key");
String result = emailTemplate.render();

这篇关于有关在GAE(Java)上设置模板框架的任何教程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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