如何使用json传递arraylist和string

时间:2015-07-13 10:30:47

标签: javascript java jquery json servlets

我试图使用ajax / json请求传递数组列表和字符串。字符串将设置为textfeilds,列表应在已定义的表格标签中设置。

这是代码。

var $myValue = $(this).attr("data-myAttribute");
                $.ajax({
                type: "GET",
                        url: "CasualLeaveController2",
                        data: $myValue,
                        dataType: "json",
                        success: function (data) {
                            if(data!=null){
                        var values = data.split(",");
                                $('#employee_no').val(values[0]);
                                $('#epf_no').val(values[1]);
                                $('#employee_name').val(values[2] + " " + values[3]);
                        }
                        else {
                        alert("No results");
                        }
                    }


                });
<table class="table table-striped table-bordered" style="width:75%" align ="center" id="mytable">
                        <thead>
                            <tr>
                                <th>Entitlement Day</th>      
                                <th>No Of used Days</th>
                                <th>Bal Avb. Days</th>
                                <th>Pending Days</th>
                                <th>Available Days</th>
                        </thead>
                        <tbody>
                            <tr style="text-align: center;">
                                <td id="entitled">$$</td>
                                <td id="used">$$</td>
                                <td id="bal">$$</td>
                                <td class="pending" data-toggle="modal" id="pend">$$</td>
                                <td class="avb_days" id="avb">$$</td>
                            </tr>

                        </tbody>
                    </table>
// more code 

    <div class="row">
                        <div class="col-sm-2">
                            <label>Employee No.</label>  
                            <input type="text" class="form-control input-sm"   id="employee_no" readonly/>
                        </div>
                        <div class="col-sm-2">
                            <label>EPF No.</label>  
                            <input type="text" class="form-control input-sm"   id="epf_no" readonly/>
                        </div>
                      <div class="col-sm-2">
                            <label>Employee Name</label>
                            <input type="text" class="form-control input-md" readonly id="employee_name"/>
                        </div>
                    </div>  

这是我的servlet方法。

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {

            int id = Integer.parseInt(request.getParameter("personid"));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            Date date = new Date();
            int year = Integer.parseInt(sdf.format(date));
            CasualLeaveService service1 = new CasualLeaveServiceImpl();
            Employee emp = service1.getEmployee(id);
            List <LeaveSummary> list1 = service1.getSummery(id, year, LeaveType.CASUAL);
            String json = new Gson().toJson(emp.getEmployeeNo() + "," + emp.getEpfNo() + "," + emp.getOtherNames() + "," + emp.getSurname()+","+list1);
            response.setContentType("text/plain");
            response.setCharacterEncoding("UTF-8");
            out.write(json);

        }
    }

数据来自db。我尝试过这个。但我无法显示整个信息。帮我。 我不能使用append方法,因为某些s中有一些特殊的类标记。所以请帮我理清这个烂摊子。 请。

0 个答案:

没有答案