在Velocity模板中定义数据库查询 [英] Defining database queries inside Velocity templates

查看:367
本文介绍了在Velocity模板中定义数据库查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究可以在我的项目中用作模板引擎的各种库,现在Apache Velocity看起来像个好人。我对它的用法有以下疑问:

I am looking at various libraries that can be used as a templating engine in my project and right now Apache Velocity looks like a good candidate. I have the following doubts regarding its usage:

是否可以在模板中指定SQL数据库查询并使用查询的返回值来填充参数?我在Velocity用户指南中找到了以下示例:

Is it possible to specify a SQL database query in a template and use the querys' return value to fill a parameter?. I found the following example in the Velocity user guide:

Hello,

#set( $result = $query.criteria("name") )
Your username is $result.

然而,本指南并没有解释执行SQL查询的问题。是否可以定义一个SQL SELECT查询,该查询返回一个值并将该值赋给模板中的变量?我想知道下面的例子是否可能?

However the guide does not explain much about executing SQL queries. Is it possible to define a SQL SELECT query which returns a value and assign this value to a variable in the template? I am wondering if something like the example below is possible?

Hello,

#set( $result = $executeQuery("SELECT name FROM user") )
Your username is $result.

如果您能对此有所了解,将不胜感激。有人提供一个例子,或者指向我可以在其中找到其他文档的位置吗?

Would be grateful if you could shed some light on this. Anyone kind enough to provide an example, or point me to a location where I can find additional documentation on this?

推荐答案

我会建议使用 http://velosurf.sourceforge.net/ ,而不是直接嵌入查询。

I would recommend something like http://velosurf.sourceforge.net/ instead of directly embedding queries.

这篇关于在Velocity模板中定义数据库查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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