开发者

Dynamic wrapPrefix for JSON result type in Struts2

开发者 https://www.devze.com 2023-01-28 23:50 出处:网络
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:

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.


Correction: The second (invalid) JSON result was:

${jsonPrefix}{ ... }


Can the json result type not parse OGNL expressions in its params?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消