Java:动态应用 Velocity 脚本

发布于 2024-12-22 19:04:42 字数 648 浏览 4 评论 0原文

我正在使用 Struts MVC 构建一个小 portlet,用于从 Web 服务搜索/列出书籍。

在我的第一次测试中,我将 Velocity Tools 集成到我的 Struts 项目中,没有出现任何问题。

最后一步是允许用户选择是否要使用速度。如果是,速度脚本将以字符串形式来自 Web 服务。 我将测试放在 JSP 页面中,以便可以应用以下算法:

List<Book> books = myWebservice.getAllBooks();
if (user.preference.vm = enabled ){
    String velocityScript = myWebservice.getARandomVelocityScript()
    renderTheBooks(velocityScript,books);
}
else{
    use JSP and/or struts tags to generate the content
}

问题出在 renderTheBooks(velocityScript,books) 行:我该如何编码?

谢谢,问候。

(我知道该架构可能看起来有点奇怪,但是请让我们讨论一下 renderTheBooks 方法。我将不胜感激您理解这一点。)

I'm building a little portlet with Struts MVC that searches/lists books from a webservice.

In my very first tests, I integrated the Velocity Tools in my Struts project with no problem.

The final step is to allow the user to choose if he wants to use velocity or not. If yes, the velocity script will come from a webservice as a String.
I put the test in a JSP page so that I can apply the following algorithm :

List<Book> books = myWebservice.getAllBooks();
if (user.preference.vm = enabled ){
    String velocityScript = myWebservice.getARandomVelocityScript()
    renderTheBooks(velocityScript,books);
}
else{
    use JSP and/or struts tags to generate the content
}

The problem is at the renderTheBooks(velocityScript,books) line : How can I code it?

Thank you, regards.

(I know that the architecture can seem a little bit odd but please, let us discuss the renderTheBooks method rather. I will appreciate you understand that point.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遇见了你 2024-12-29 19:04:42

Velocity 类为您提供静态便捷方法:

public static boolean evaluate(Context context,
                           Writer writer,
                           String logTag,
                           Reader reader)

假设阅读器包含您的速度模板,并且速度配置正确
您可以使用此方法对其进行评估

Velocity class offers you static convenience method:

public static boolean evaluate(Context context,
                           Writer writer,
                           String logTag,
                           Reader reader)

Assuming that reader contains your velocity template, and velocity was configured correctly
you can evaluate it using this method

好多鱼好多余 2024-12-29 19:04:42
#foreach( $book in $books )
    $book
#end

这样您就可以快速渲染书籍列表。

#foreach( $book in $books )
    $book
#end

This way you can render list of books in velocity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文