Defining database queries inside Velocity templates

北城余情 提交于 2019-12-21 17:39:33

问题


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:

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.

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?


回答1:


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




回答2:


Velocity is a very lightweight templating engine, it has very little functionality by itself. It works by interpolating variables defined (by you) in a context into a template file. By default, the context is empty, meaning that there are no variables for the template to use. Velocity doesn't allow instantiating new objects of custom classes, all it allows you to use is string and number literals, plus maps and lists:

#set ($n = 5)
#set ($s = 'Hello New World!')
#set ($m = {'four' : 4, 'five' : $n})
#set ($a = ['x', 'y', 'z'])

You're responsible for making Velocity "smart" by populating the context with useful objects before interpolating. As a starting point, you can use some of the Velocity Tools. You can add your own tools, like an SQL tool that lets you run queries. Or you can use a higher level framework that uses velocity and which already offers a rich set of tools available in templates.

So, to answer your question, you can execute any SQL statement you want, as long as you add in the context an object that can execute such statements. You should read more about how to properly use Velocity in their developer guide.




回答3:


This following is for HQL (you can try with others using this as example)

General example showing how to display the first 5 results of a given query:

#set($hql = "<query here>")
#set($results = $xwiki.searchDocuments($hql, 5, 0))
#foreach ($item in $results)
 * $item
#end

The examples below will show you various HQL queries that you can write. Simple Query

Displays all documents who have been created by the user XWiki.JohnDoe

#set($hql = "where doc.creator='XWiki.JohnDoe'")

Additonal Info

you may want to look at this link



来源:https://stackoverflow.com/questions/14374811/defining-database-queries-inside-velocity-templates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!