ajax json struts JSP傳遞消息到action返回數據到JSP

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>
相關文章
相關標籤/搜索