Struts2如何返回JSON响应

use*_*206 12 java json action struts2 gson

我目前正在创建一个Web应用程序,用户可以从数据库中获取标记为JSON,

这是我的struts动作

public String execute(){


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

    return "success";
}
Run Code Online (Sandbox Code Playgroud)

更新:

tagsAsJson已经处于JSON格式的所有我想要的是只返回,而不是整个集体诉讼本身.

它返回这样的东西

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

[{"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 "}]
Run Code Online (Sandbox Code Playgroud)

如何返回tagsAsJsonas ar JSON响应?因为JSON响应将由客户端代码使用.

Pla*_*ton 21

使用Struts "JSON插件".

很简单,三个步骤:

只需将它包含在您的maven项目中即可

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-json-plugin</artifactId>
    <version>${version.struts2}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

声明您希望作为JSON字符串返回的字段,如操作字段,提供getter和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;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,将它放在您的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>
Run Code Online (Sandbox Code Playgroud)

注意<param name="root">jsonString</param>.这段xml告诉Struts2这个确切的属性应该被视为JSON序列化的根.因此,只有命名属性(以及下面,如果它是地图或任何地方)将在JSON响应中返回.

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

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


小智 13

尝试使用响应的PrintWriter.

Java的

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

      response.setContentType("application/json");
      response.getWriter().write(jsonString );

      return null;
   }
Run Code Online (Sandbox Code Playgroud)


小智 2

在 Action 类中放置以下代码。

  public class Struts2Action extends ActionSupport
  {     

    public String jsonString="";

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

      return "success";
   }
  }
Run Code Online (Sandbox Code Playgroud)

在JSP中输入以下代码

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title>Struts Rais</title>

<s:property value="jsonString"/><br />
Run Code Online (Sandbox Code Playgroud)

如果您想操作 JSON 数据,您可以在 var<s:set>标记中捕获数据并访问整个页面中的变量,这将打印 JSON 数据。

  • 如果 OP 正在寻找 JSON 响应,那么他们很可能不需要 HTML,需要设置内容类型等。 (5认同)