ACTIONjavascript
package actions; import com.opensymphony.xwork2.ActionSupport; import net.sf.json.JSONObject; import pojo.Goods; import service.GoodsService; public class GoodsAction extends ActionSupport { //定义msg数组传值到ajax private String []msg = new String[]{"null","null","null","null","null","null"}; public String[] getMsg() { return msg; } public void setMsg(String[] msg) { this.msg = msg; } /** * 查询出返回主界面 * 将json转string * @return 返回string msg数组 * @throws Exception */ public String showmygoods() throws Exception{ // String str = "9787121321450"; // long id = Long.parseLong(str); // goods.setGoodsId(id); goods=goodsService.find_goods(goods.getGoodsId()); if(goods !=null) { try { JSONObject jsonObject = JSONObject.fromObject(goods); msg[0] = jsonObject.getString("goodsId"); msg[1] = jsonObject.getString("goodsName"); msg[2] = jsonObject.getString("goodsType"); msg[3] = jsonObject.getString("goodsPrice"); msg[4] = jsonObject.getString("goodsFrom"); msg[5] = jsonObject.getString("goodsAmount"); //json获取指定键值数据 字符串进行拼接 // msg = jsonObject.getString("goodsId")+","+jsonObject.getString("goodsName")+","+ // jsonObject.getString("goodsType")+","+jsonObject.getString("goodsPrice")+","+ // jsonObject.getString("goodsFrom")+","+jsonObject.getString("goodsAmount"); // System.out.println(msg); goods=null; } catch (Exception e) { e.printStackTrace(); } } return SUCCESS; }
STRUTS.XMLhtml
<!-- jquery,json方式 --> <action name="showmygoods" method="showmygoods" class="goodsAction"> <result type="json"> <!--是否去掉null值,默认为false--> <param name="excludeNullProperties">true</param> <param name="root">msg</param> </result> <!--<result name="success" type="json"></result>--> </action>
JSPjava
<script type="text/javascript" src="assets/js/jquery-1.10.2.js"></script> <script> <%--jax+struts2+JQuery+json--%> $(document).ready(function() { $("#ajaxA").click(function() { // var obj = $('#rate').prop("value"); $.ajax({ type : 'post',//请求方式 url : 'showmygoods.action',//请求路径 // data : {//传给action的参数,rate在action中必须有get,set方法 // goodsid : obj // }, // dataType : 'json',//以json格式封装数据 //无异常时执行的方法 success : function(msg) { // var html = ''; // // html = html + '<thead>'; // html = html + '<tr>'; // html = html + '<th>商品编号</th>'; // html = html + '<th>商品名称</th>'; // html = html + '<th>商品分流</th>'; // html = html + '<th>商品价格(元)</th>'; // html = html + '<th>商品产地</th>'; // html = html + '<th>商品数量(件)</th>'; // html = html + '</tr>'; // html = html + '</thead>'; // $("#mytable").html(html); if (!(msg[0]=='null')) { //判断数组非空 // html = html + '<tr>'; // html = html + '<td>' + msg[0] + '</td>'; // html = html + '<td>' + msg[1] + '</td>'; // html = html + '<td>' + msg[2] + '</td>'; // html = html + '<td>' + msg[3] + '</td>'; // html = html + '<td>' + msg[4] + '</td>'; // html = html + '<td>' + msg[5] + '</td>'; // html = html + '</tr>'; var addRow = "<tr><td>"+msg[0]+"</td><td>"+msg[1]+"</td><td>"+msg[2]+"</td><td>"+ msg[3]+"</td><td>"+msg[4]+"</td><td>"+msg[5]+"</td></tr>" $("#mytable tbody").append(addRow); } }, //出现异常时执行的方法 error : function(data) { $("#mytable").html("出现异常"); } }); }); }); </script> </head> <body> <button id="ajaxA">ajax刷新</button> <table class="table table-striped table-bordered table-hover" id="mytable"> <thead> <tr> <th>商品编号</th> <th>商品名称</th> <th>商品分类</th> <th>商品价格(元)</th> <th>商品产地</th> <th>商品数量(件)</th> </tr> </thead> <tbody> <%--<tr>--%> <%--<td>${goods.goodsId}</td>--%> <%--<td>${goods.goodsName}</td>--%> <%--<td>${goods.goodsType}</td>--%> <%--<td>${goods.goodsPrice}</td>--%> <%--<td>${goods.goodsFrom}</td>--%> <%--<td>${goods.goodsAmount}</td>--%> <%--</tr>--%> </tbody> </table> </div>