只有最后一条记录显示在Struts动作发送的Ajax中

时间:2016-07-23 10:26:01

标签: java jquery ajax struts2

这是我在JSP中生成数据表的代码。在我的Struts动作类中,我使用了包含两个记录的列表,但在我的JSP中它只显示了对象中的最后一条记录

Struts动作类:

 @ParentPackage("json-default")
        public class StrutsPaginationAction extends ActionSupport {

            private static final long serialVersionUID = 1L;
            private int iTotalRecords;
            private int iTotalDisplayRecords;
            private String sEcho;
            private String sColumns;
            private List<SmsDTO> aaData;
            private List<SmsDTO> personsList;
        DBCLass d=new DBCLass();

            public StrutsPaginationAction() {

                SmsDTO smsDTO=new SmsDTO();


                    **personsList=d.smsResult();**






                System.out.println(personsList.size());
                setiTotalDisplayRecords(personsList.size());

                setiTotalRecords(personsList.size());
                setAaData(personsList);

            }

            @Action(value = "/strutsPaginationAction", results = { @Result(name = "success", type = "json") })
            public String execute() throws Exception {
                return ActionSupport.SUCCESS;
            }

            public List<SmsDTO> getPersonsList() {
                return personsList;
            }

            public void setPersonsList(List<SmsDTO> personsList) {
                this.personsList = personsList;
            }

            public int getiTotalRecords() {
                return iTotalRecords;
            }

            public void setiTotalRecords(int iTotalRecords) {
                this.iTotalRecords = iTotalRecords;
            }

            public int getiTotalDisplayRecords() {
                return iTotalDisplayRecords;
            }

            public void setiTotalDisplayRecords(int iTotalDisplayRecords) {
                this.iTotalDisplayRecords = iTotalDisplayRecords;
            }

            public String getsEcho() {
                return sEcho;
            }

            public void setsEcho(String sEcho) {
                this.sEcho = sEcho;
            }

            public String getsColumns() {
                return sColumns;
            }

            public void setsColumns(String sColumns) {
                this.sColumns = sColumns;
            }

            public List<SmsDTO> getAaData() {
                return aaData;
            }

            public void setAaData(List<SmsDTO> aaData) {
                this.aaData = personsList;
            }
        }

我的Ajax代码:

    <script type="text/javascript">

    $(document).ready(function(){

        $("#example").dataTable( {
            "bProcessing": false,
            "bServerSide": false,
            "sort": "phone",
            "sAjaxSource": "strutsPaginationAction",
            "aoColumns": [
                { "mData": "phone" },
                { "mData": "studentname" },
                { "mData": "ref" },



               /*  { "mData": "phone" },
                { "mData": "start_date" },
                { "mData": "salary" }, */

            ]
        } );

    } );

    </script>
    </head>
    <body>
    <h2 >Struts 2 pagination using DataTables<br><br></h2>
    <table width="70%" style="border: 3px;background: rgb(243, 244, 248);"><tr><td>
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>Phone</th>
                    <th>Name</th>
                    <th>Ref</th>
                    <!-- <th>Phone</th>
                    <th>Start Date</th>
                    <th>Salary</th> -->
                </tr>
            </thead>       
        </table>
        </td></tr></table>
    </body>

1 个答案:

答案 0 :(得分:0)

这是数据库代码的问题....以前我是实例化对象里面的循环,这是它重复的原因

 public ArrayList<SmsDTO> getReulst() {
            /*ResultSet rs = null;
            Connection conn = null;
            PreparedStatement pstmt = null;
            ArrayList<SmsDTO> ar = new ArrayList<SmsDTO>();
            SmsDTO sms = null;
            try {

                conn = getConnection();
                String query = "select pt.P_MOBILE,st.S_FIRSTNAME,st.REF_ID  from parent_info pt join student_info st on pt.REF_ID = st.REF_ID  where st.S_CLASS_TO_JOIN = 10;";
                pstmt = conn.prepareStatement(query); // create a statement
                rs = pstmt.executeQuery();
                // extract data from the ResultSet
                while (rs.next()) {


sms = new SmsDTO();
-------------------

                    long phone = rs.getLong(1);
                    sms.setPhone(phone);
                    String student_name = rs.getString(2);
                    sms.setStudentname(student_name);
                    String ref = rs.getString(3);
                    sms.setRef(ref);
                    ar.add(sms);

                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    rs.close();
                    pstmt.close();
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return ar;*/
    }
    }