Struts2中JSON结果类型的动态wrapPrefix
我有一个 struts2 操作,其结果类型当前正在运行。我还能够成功添加静态“wrapPrefix”:
<action name="example_*" class="example.ExampleAction" method="{1}">
<result name="success" type="json">
<param name="wrapPrefix">test</param>
</result>
<result name="error" type="chain">jsonError</result>
</action>
正如预期的那样,example_list.action 的 JSON 结果是(其中 { ... } 表示原始结果 JSON):
测试{ ... }
我希望能够使“wrapPrefix”动态化。我已经尝试了以下操作,无论是否带有“parse”参数:
<action name="example_*" class="example.ExampleAction" method="{1}">
<result name="success" type="json">
<param name="parse">true</param>
<param name="wrapPrefix">${jsonPrefix}</param>
</result>
<result name="error" type="chain">jsonError</result>
</action>
在ExampleAction中,我添加了getter:
public String getJsonPrefix() {
return "test";
}
但是,现在生成的JSON是:
${jsonPrefix}&&{ ... }
json 结果类型不能解析其参数中的 OGNL 表达式吗?是否需要一些其他配置来创建效果?我想使用(或)现有的自动对象模型到 JSON 转换,而不是创建整个自定义 JSON 字符串。
I have a struts2 action with a json result type that is currently working. I was able to successfully add a static "wrapPrefix" as well:
<action name="example_*" class="example.ExampleAction" method="{1}">
<result name="success" type="json">
<param name="wrapPrefix">test</param>
</result>
<result name="error" type="chain">jsonError</result>
</action>
As expected, the JSON result for example_list.action was (where { ... } indicates the original result JSON):
test{ ... }
I want to be able to make the "wrapPrefix" dynamic. I've tried the following, both with and without the "parse" param:
<action name="example_*" class="example.ExampleAction" method="{1}">
<result name="success" type="json">
<param name="parse">true</param>
<param name="wrapPrefix">${jsonPrefix}</param>
</result>
<result name="error" type="chain">jsonError</result>
</action>
In the ExampleAction, I added the getter:
public String getJsonPrefix() {
return "test";
}
However, now the resulting JSON is:
${jsonPrefix}&&{ ... }
Can the json result type not parse OGNL expressions in its params? Is some other configuration needed to create the effect? I want to use the (or an) existing automatic object model-to-JSON conversion, not create an entire custom JSON string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更正:第二个(无效)JSON 结果是:
Correction: The second (invalid) JSON result was:
不,它无法解析任何参数中的 OGNL 表达式。您可以尝试对其进行子类化,但老实说,您可能更容易制作该类的本地副本并直接修改它,因为它不是为扩展而设计的。
No, it cannot parse OGNL expressions in any of its parameters. You could try subclassing it, but honestly, it would probably be easier for you to just make a local copy of the class and modify it directly, since it wasn't designed for extension.