如何从struts 2中的操作返回JSON

时间:2016-06-13 09:17:54

标签: json struts2

我想从struts动作类返回一个大的JSON响应。 我在eclipse中创建了一个动态Web项目,而没有使用任何其他工具的maven。

所以我需要为此做些什么。

1 个答案:

答案 0 :(得分:0)

按照步骤

第1步pom.xml

<!-- Struts 2 -->
<dependency>
      <groupId>org.apache.struts</groupId>
  <artifactId>struts2-core</artifactId>
  <version>2.1.8</version>
</dependency>

<!-- Struts 2 JSON Plugins -->
<dependency>
      <groupId>org.apache.struts</groupId>
  <artifactId>struts2-json-plugin</artifactId>
  <version>2.1.8</version>
</dependency>

如果您不使用maven,可以手动添加jar并将它们复制到WEB-INF / lib文件夹中。

Step2采取行动

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.Action;

public class JSONDataAction{

private String string1 = "A";
private String[] stringarray1 = {"A1","B1"};
private int number1 = 123456789;
private int[] numberarray1 = {1,2,3,4,5,6,7,8,9};
private List<String> lists = new ArrayList<String>();
private Map<String, String> maps = new HashMap<String, String>();

//no getter method, will not include in the JSON
private String string2 = "B";

public JSONDataAction(){
    lists.add("list1");
    lists.add("list2");
    lists.add("list3");
    lists.add("list4");
    lists.add("list5");

    maps.put("key1", "value1");
    maps.put("key2", "value2");
    maps.put("key3", "value3");
    maps.put("key4", "value4");
    maps.put("key5", "value5");
}

public String execute() {
           return Action.SUCCESS;
    }

public String getString1() {
    return string1;
}

public void setString1(String string1) {
    this.string1 = string1;
}

public String[] getStringarray1() {
    return stringarray1;
}

public void setStringarray1(String[] stringarray1) {
    this.stringarray1 = stringarray1;
}

public int getNumber1() {
    return number1;
}

public void setNumber1(int number1) {
    this.number1 = number1;
}

public int[] getNumberarray1() {
    return numberarray1;
}

public void setNumberarray1(int[] numberarray1) {
    this.numberarray1 = numberarray1;
}

public List<String> getLists() {
    return lists;
}

public void setLists(List<String> lists) {
    this.lists = lists;
}

public Map<String, String> getMaps() {
    return maps;
}

public void setMaps(Map<String, String> maps) {
    this.maps = maps;
}

}

第3步Struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE struts PUBLIC
 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<package name="default" namespace="/" extends="json-default">
  <action name="getJSONResult" 
       class="com.mkyong.common.action.JSONDataAction">
       <result type="json" />
  </action>
 </package>

 </struts>

运行项目并输出http://localhost:8080/Struts2Example/getJSONResult.action

output

{
"lists":["list1","list2","list3","list4","list5"],
"maps":
 {
  "key4":"value4","key3":"value3","key5":"value5","key2":"value2","key1":"value1"
},
"number1":123456789,
"numberarray1":[1,2,3,4,5,6,7,8,9],
"string1":"A",
"stringarray1":["A1","B1"]
}

只需在struts.xml中确保您的工作区扩展了json工作区

 <package name="default" namespace="/" extends="json-default">