将速度响应转换为 JSON
我正在使用 struts 2 和velocity 模板来生成 JSON 响应。 现在的问题是响应不是使用某些速度 JSON 插件生成的 它只是一个字符串,一旦速度完成解析和渲染后就会出现 响应,在客户端我执行 eval 来获取从字符串到 JSON 的响应。
我真正需要的是速度或支柱方面的一些解决方案,一旦结果是 由速度生成,框架应该调用我的 API,我可以使用我自己的逻辑将 vm 文件的响应输出转换为 JSON。如何实现这一目标?
例如:
- 在使用 JavaScript 的浏览器上,我设计了一个树形小部件,用于以树形结构显示注释。
- 假设用户点击评论按钮。
- 我的 UI 小部件将触发 AJAX 来获取评论数据。
- 该请求被 STRUTS 2 框架拦截。
- 例如,它将调用
getComments()
操作 API,并使用注释对象(例如 cmt)填充 arrayList。 - 现在响应由速度模板(*.vm)处理。
现在在虚拟机中我正在编写这样的代码:
{ "评论数据" : [ #set($sep="") #foreach($c in $cmt) $九月 { “commentText”:$c.getText() } #set($sep=",") #结尾 }
现在最终的响应可能是这样的:
{ "评论数据" : [ { "commentText" : "这是评论 1" }, { "commentText" : "这是评论 2" }, { "commentText" : "这是评论 3" }, { "commentText" : "这是评论 4" }` ] }
现在这可能看起来像 JSON,但它不是严格的 JSON;我的意思是如果我错过了 然后在 JavaScript 客户端的某个
,
处,我的 eval 可能会失败或JSON.parse()
会失败,但在速度模板上我现在知道 JSON 是否格式错误。因此,一旦生成上述速度模板,我需要一些控制,我可以在其中编写一些 Java 代码来对响应进行一些验证。
我发现我使用速度模板生成 JSON 输出(实际上是一个看起来像 JSON 的字符串)的方法可能是错误的。但我仍然需要处理我编写的每个速度模板的响应。
I am using struts 2 and velocity templates to generate JSON response.
Now the catch is the response is not generated using some velocity JSON plugin
it's just a String that comes out once velocity is done with its parsing and rendering of
response, and on client side I do eval to get the response from string to JSON.
What I really need is some solution on velocity's or struts' side where, once the result is
generated by velocity, the framework should call my API where I can convert the response output of vm file into JSON using my own logic. How do achieve this?
For example:
- On browser using JavaScript I have designed a tree widget that I use for displaying comments in tree structure.
- Say user clicks on comments button.
- My UI widget will fire an AJAX to get data for comments.
- This request is intercepted by STRUTS 2 framework.
- It will call, say,
getComments()
action API and will populate an arrayList with comment object say cmt. - Now the response is handled by a velocity template(*.vm).
Now in vm I am writing code like this:
{ "CommentsData" : [ #set($sep="") #foreach($c in $cmt) $sep { "commentText" : $c.getText() } #set($sep=",") #end }
Now the final response may turn out like this:
{ "CommentsData" : [ { "commentText" : "This is comment 1" }, { "commentText" : "This is comment 2" }, { "commentText" : "This is comment 3" }, { "commentText" : "This is comment 4" }` ] }
Now this may look like JSON, but its not strict JSON; I mean if I miss
some,
somewhere then on client side in JavaScript my eval might fail orJSON.parse()
will fail, but on velocity template I have now clue if JSON is malformed.So once the above velocity template is generated I need some control, where I can write some Java code to do some validations on the response.
I see that my approach to use velocity template to generate JSON output (actully a String that looks like JSON) may be wrong. But still I need to handle the response of every velocity template I have written.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定你如何使用速度。我们在输出 JSON 时不使用速度;我们只需创建一个 JSON 可转换对象并使用response.write(jsonObject.toJson()) 直接从控制器输出它。这样,始终会生成正确的 JSON。
Not sure how you are using velocity. We don't use velocity when outputting JSON; we just create a JSON convertible object and output it directly from controllers using
response.write(jsonObject.toJson())
. This way, proper JSON is always generated.