Struts2如何返回JSON响应 [英] Struts2 How to Return a JSON Response

查看:161
本文介绍了Struts2如何返回JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个Web应用程序,用户可以从数据库中以JSON的形式获取标签, p

这里是我的struts动作

  public String execute(){


Gson gson = new Gson();
String tagsAsJson = gson.toJson(audioTaggingService.findTagsByName(q));
System.out.println(tagsAsJson);

返回成功;

更新:

tagsAsJson 已经是JSON格式,我只需要返回它,而不是整个类动作本身。



它返回类似这样的内容

这是我想要返回给用户的数据

  [{id:2,name:Dubstep,description:Dub wob wob},{id:3,name:BoysIIMen ,description:A 1990s Boy Band},{id:4,name:Sylenth1,description:FLStudio的VST插件}] 

如何返回 tagsAsJson 作为ar JSON响应?因为那个JSON响应将被客户端代码使用。

使用Struts JSON Plugin



很简单,三步:

只需将它包含在你的maven项目中就可以了

 < ;依赖性> 
< groupId> org.apache.struts< / groupId>
< artifactId> struts2-json-plugin< / artifactId>
< version> $ {version.struts2}< / version>
< /依赖关系>

声明您希望返回的字段为JSON字符串,例如您的操作字段, getter和setter。

  public class Struts2Action extends ActionSupport {
$ b $ private String jsonString;

public String execute(){
Gson gson = new Gson();
jsonString = gson.toJson(audioTaggingService.findTagsByName(q));

返回成功;
}

public String getJsonString(){
return jsonString;
}

public void setJsonString(String jsonString){
this.jsonString = jsonString;


最后,把它放在你的XML中:

 < action name =someJsonActionclass =com.something.Struts2Action> 
< result type =json>
< param name =noCache> true< / param>
< param name =excludeNullProperties> true< / param>
< param name =root> jsonString< / param>
< / result>
< / action>

请注意< param name =root> jsonString< / PARAM> 。这段XML告诉Struts2这个确切的属性应该被认为是JSON序列化的根。所以只有命名的属性(和下面的,如果它是一个映射或任何其他)将在JSON响应中返回。



感谢JSON插件,内容类型将是正确的。

JSON插件文档在这里: http://struts.apache.org/release/2.3.x/docs/json-plugin.html


I am currently creating a web-application where the users can fetch tags from the database as JSON,

here is my struts action

public String execute(){


    Gson gson = new Gson();
    String tagsAsJson = gson.toJson(audioTaggingService.findTagsByName(q));
    System.out.println(tagsAsJson);

    return "success";
}

UPDATE:

The tagsAsJson is already in a JSON format all I want is to return only that, and not the whole class action itself.

It returns something like this

This is the data I want to return to the user

[{"id":2,"name":"Dubstep","description":"Dub wob wob"},{"id":3,"name":"BoysIIMen","description":"A 1990s Boy Band"},{"id":4,"name":"Sylenth1","description":"A VST Plugin for FLStudio "}]

How do I return the tagsAsJson as a r JSON esponse? since that JSON response will be used by the client side code.

解决方案

Use the Struts "JSON Plugin".

Quite easy, three steps:

Just include it in your maven project like this

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-json-plugin</artifactId>
    <version>${version.struts2}</version>
</dependency>

Declare the field you'd like to return as JSON string like the field of your action, provide the getter and setter.

public class Struts2Action extends ActionSupport {

    private String jsonString;

    public String execute() {
        Gson gson = new Gson();
        jsonString = gson.toJson(audioTaggingService.findTagsByName(q));

        return "success";
    }

    public String getJsonString() {
        return jsonString;
    }

    public void setJsonString(String jsonString) {
        this.jsonString = jsonString;
    }
}

And, finally, put this in your XML:

<action name="someJsonAction" class="com.something.Struts2Action">
    <result type="json">
        <param name="noCache">true</param>
        <param name="excludeNullProperties">true</param>
        <param name="root">jsonString</param>
    </result>
</action>

Pay attention to <param name="root">jsonString</param>. This piece of xml tells Struts2 that this exact property should be considered as a root for JSON serialization. So only the named property (and below, if it's a map or whatsoever) will be returned in a JSON response.

Thanks to the JSON Plugin the content type will be correct.

"JSON Plugin" documentation is here: http://struts.apache.org/release/2.3.x/docs/json-plugin.html

这篇关于Struts2如何返回JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆