2018-5-8
参考了很多博客,自己亲测可用
导包
struts2-json-plugin-2.1.8.1.jar
json-lib-2.1.jar,
在默认的情况下,不能实现action返回类型为json,此时需要改变默认的继承包,用如下注解@ParentPackage(“json-default”)来改变继承的包为json-default.
@Namespace("/api")
@ParentPackage("json-default")
public class ChannelAPIAction extends BaseAction {
// private JSONArray json = new JSONArray();
/**
*
*/
private static final long serialVersionUID = 1L;
@Autowired
private ProductsService productsService;
@Action(value = "/findAll", results = { @Result(name = "json", type = "json") })
public String list() {
System.out.println("api/list");
List<Products> list = productsService.list();
JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
JSONObject object = new JSONObject();
object.put("code", 200);
object.put("data", json);
AjaxResponse.ajaxPrintByJson(object);
return "json";
}
}
AjaxResponse.ajaxPrintByJson(object);是工具类
public static void ajaxPrintByJson(Object content) {
HttpServletResponse response = responseCommon();
try {
response.setContentType("text/json;charset=UTF-8");
java.io.PrintWriter out = response.getWriter();
out.print(content);
out.flush();//这里写的不严谨
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static HttpServletResponse responseCommon(){
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
return response;
}
结果展示
到这里本来以为大功告成,到后台发现报错 org.apache.commons.lang.xwork.StringUtils
这里有个解决的方案比较好https://blog.csdn.net/qq_27109081/article/details/47127971
思想就是:你少哪个类,我就该你加上。
2018-5-30
修改
以上是把json写入response,这种方式没有必要配置json-default,直接就可以返回json
json-default是可以自动把对象,数组,列表等转化生成json,与springMVC中的@ResponseBody有点像
@ParentPackage("json-default")
public class DemoAction extends BaseAction {
// private JSONArray json = new JSONArray();
private List<Products> list = new ArrayList<Products>();
private Map<String, String> map = new HashMap<String, String>();
private Products products = new Products();
@Autowired
private ProductsService productsService;
@Action(value = "/api/findAll", results = { @Result(name = "json", type = "json") })
public String list() {
System.out.println("api/list");
list = productsService.list();
map.put("test1", "value1");
map.put("key1", "value1");
products.setCName("cName");
products.setPrice("123");
products.setUsername("userName");
products.setProductName("productName");
// JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
// JSONObject object = new JSONObject();
// object.put("code", 200);
// object.put("data", json);
// AjaxResponse.ajaxPrintByJson(object);
// AjaxResponse.responseOut(object);
return "json";
}
格式不对,直接复制的google浏览器返回的就送数据(json-view插件美化之后)
{
list: [
{
channelName: "asd",
price: "6301",
productName: "产品一",
username: "qds001"
},
{
channelName: "asd",
price: "6532",
productName: "产品一",
username: "qds001"
}
],
map: {
test1: "value1",
key1: "value1"
},
products: {
clName: "cName",
price: "123",
productName: "productName",
username: "userName"
}
}
上面的返回结果依然不完善,因为返回的是类中所有有getter方法的属性,
需要进一步配置
@Action(value = "/api/findAll", results = { @Result(name = "json", type = "json",params={"root","list"}) })
这种方式返回的是只有list数据
//可以根据自己子想返回的数据自行配置
@Action(value = "/api/findAll", results = { @Result(name = "json", type = "json",params={"includeProperties","list\\[\\d+\\]\\.cName"}) })
返回list列表中cName
@Action(value = "/api/findAll", results = { @Result(name = "json", type = "json",params={"includeProperties","list\\[\\d+\\]\\.channelName,map.*"}) })
返回list特定字段和map
还有一个配置是excludeProperties使用和includeProperties差不多,一个是包含,一个是排除
以上是基于注解的配置,xml配置请参考官方文档
http://struts.apache.org/plugins/json/
如果返回的json数据格式比较简单,则可以使用这种方式