Struts2返回json格式数据踩坑记录
2016-12-27 20:01
483 查看
事件起因
昨天提测修改冻结/解冻银行卡样式的功能,微姐测试过程中发现调用ajax请求耗时过长,今天来排查,发现浏览器请求/finance/ajax/freeze/ajaxGetShopLists时,对应的后端方法getShopList()莫名其妙地执行了两边,并且返回给浏览器的Json字符串如下:{ "accountId": null, "accountInfoBean": { "accountId": null, "accountName": null, "balance": null, "bankAccountEffective": 0, "bankAccountName": null, "bankName": null, "bankNumber": null, "canWithDraw": null, "canWithDrawNow": null, "canWithdrawAmount": null, "frozenReason": null, "hadTransferTxToday": false, "nextSettleDate": null, "settleType": 0, "status": 0, "withDrawNowLimitBySubAccount": null, "withdrawRuleString": null }, "admin": false, "code": 200, "customerId": 401**976, "customerList": null, "customerName": "肖*", "message": null, "mobile": "1301***5995", "msg": { "customerName": "肖**", "currentShopId": 0, "accountId": null, "shopList": [ { "accountId": "210*******021", "customerId": 0, "displayName": "公司账户", "shopId": 0, "statusMessage": "" } ], "code": 200, "mobile": "130****5995" }, "productCode": 1, "selectedShopName": "", "shopAccountBeanList": [ { "accountId": "2100*****021", "customerId": 0, "displayName": "公司账户", "shopId": 0 } ], "shopId": 0, "shopList": "success", "shopListJson": "[]" }
而对应的方法如下:
public String getShopList() { if (flag) { loginAccountProfile = getLoginAccountProfile(); //初始化门店信息 code = SUCCESS_CODE; initShopInfo(); getAccountIdStatus(shopAccountBeanList); //获取客户名以及电话 getCustomerNameAndMobile(); msg.put("shopList", shopList); msg.put("currentShopId", currentShopId); msg.put("accountId", accountId); msg.put("mobile", dealWithMobileNo(mobile)); msg.put("customerName", customerName); msg.put("code", code); flag = false; }else{ logger.info("-------flag is false"); } return SUCCESS; }
对应的Struts配置如下
<package name="freezeAjax" namespace="/finance/ajax/freeze" extends="union-bill"> <action name="ajaxGetShopLists" class="com.*****.AjaxGetShopList" method="getShopList"> <result type="json" name="success"></result> </action> </package>
从上面返回的Json字符串内容来看,返回字段并不是getShopList()方法中指定的字段,而是包含了com.*****.AjaxGetShopList类中所有get方法对应的字段。
初步推测getShopList()执行两次的过程如下:
后端struts框架接收到浏览器发送的请求,找到配置对应的action,将请求交给action对应的method来处理
action对应的method处理完成后,根据xml中配置的内容返回结果给浏览器。网上搜索struts返回json的配置格式如下
<action name="jsonAction" class="com.ljq.action.JsonAction"> <result type="json"> <!-- 此处将reslut的值返回给客户端,root的值对应要返回的值的属性result 注意:root为固定写法,否则不会把result的值返回给客户端 --> <param name="root">result</param> </result> </action>
所以推测是由于xml配置有问题,导致返回结果给浏览器时,将com.*****.AjaxGetShopList类中所有的get方法都执行了一遍。
尝试修改
将struts配置文件修改如下<package name="freezeAjax" namespace="/finance/ajax/freeze" extends="union-bill"> <action name="ajaxGetShopLists" class="com.*****.AjaxGetShopList" method="getShopList"> <!-- <result type="json"></result> --> <result type="json"> <param name="root">msg</param> </result> </action> </package>
此时getShopList()方法只被调用一次,返回给浏览器的json格式如下:
{ "customerName": "饶总", "currentShopId": 0, "accountId": null, "shopList": [ { "accountId": "2100*****214", "customerId": 0, "displayName": "公司账户", "freezeStatus": 0, "shopId": 0, "statusMessage": "" } ], "code": 200, "mobile": "183****5421" }
推测成立。在AjaxGetShopList类中增加一个getTestString()方法,使用之前错误的配置方式,无论AjaxGetShopList类中存不存在testString字段,返回给浏览器的json字符串中会多一个testString字段。
相关文章推荐
- Struts2返回json格式数据踩坑记录
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及非文件上传的部分
- Struts2不用插件返回json格式数据
- struts2返回json格式数据
- struts2 json格式返回数据
- 使用Ajax发送Struts2请求返回JSON数据格式
- 使用jQuery框架的ajax技术,在Struts2返回json格式的数据
- 关于jQuery获取Action返回的JSON数据 项目真实案例 记录(Struts2)
- struts2的action中返回json格式的数据
- struts2、servlet中返回json数据格式
- struts2 + ajax(从后台获取json格式的数据返回到前端,然后前端用jquery对json数据进行解析)
- SSH:Struts2的Action如何返回JSON格式的数据?
- struts2返回json格式的数据
- jquery调用struts2,返回script/text/json格式的数据
- struts2 从action中返回json格式数据:
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及文件上传的部分
- 关于Struts2返回XML,JSON格式配置记录
- Struts2 使用ajax访问action,返回json格式数据,提示error:There is no result type defined for type 'json'
- 关于jQuery获取Action返回的JSON数据 项目真实案例 记录(Struts2)
- java 实现自动编译成json struts2 中不用配置json等jar包来实现低耦合,低入侵式ajax访问返回数据