简体   繁体   English

如何将对象列表传递给Spring Controller以进行多行插入?

[英]How to pass list of objects to Spring Controller for multiple row inserts?

There is a jsp which outputs a HTML5 page. 有一个jsp输出HTML5页面。 The HTML5 has two buttons - "Add" and "Save". HTML5有两个按钮-“添加”和“保存”。 HTML5 local storage feature is used to store offline data. HTML5本地存储功能用于存储脱机数据。

"Add" button adds a record when clicked. 单击“添加”按钮可添加一条记录。 So if user fills the fields on the jsp page 5 times and clicks the "Add" button 5 times, 5 records are added to the HTML5 table. 因此,如果用户填充jsp页面上的字段5次并单击“添加”按钮5次,则5条记录将添加到HTML5表中。 When "Add" button is clicked, a javascript is run which adds the record to a result set. 单击“添加”按钮后,将运行Javascript,将记录添加到结果集中。 So for 5 clicks the javascript result set contains 5 records. 因此,对于5次单击,javascript结果集包含5条记录。

"Save" button, when clicked, must insert all the 5 records from the javascript result set to the Oracle database. 单击“保存”按钮时,必须将javascript结果集中的所有5条记录插入到Oracle数据库中。 To do this the list of records from result set must be passed to the Spring controller. 为此,必须将结果集中的记录列表传递给Spring控制器。

The Spring controller has been coded using batchUpdate api. Spring控制器已经使用batchUpdate API进行了编码。

public void insertListOfPojos(final List<MyPojo> myPojoList) {

    String sql = "INSERT INTO "
        + "MY_TABLE "
        + "(FIELD_1,FIELD_2,FIELD_3) "
        + "VALUES " + "(?,?,?)";

    getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {

        @Override
        public void setValues(PreparedStatement ps, int i)
            throws SQLException {

            MyPojo myPojo = myPojoList.get(i);
            ps.setString(1, myPojo.getField1());
            ps.setString(2, myPojo.getField2());
            ps.setString(3, myPojo.getField3());

        }

        @Override
        public int getBatchSize() {
            return myPojoList.size();
        }
    });

}

The question is - How do you pass the records from the javascript result set to the Spring controller so that it is available to controller's "myPojoList" when the "Add" button is clicked? 问题是-如何将记录从javascript结果集中传递给Spring控制器,以便在单击“添加”按钮时可将其提供给控制器的“ myPojoList”?

I have couple of thoughts 我有几点想法

  1. Form that 5 records into a json string and post to the controller and unmarshall the json to object and store in Oracle. 将这5条记录形成一个json字符串,然后发布到控制器,然后将json编组为对象并存储在Oracle中。

  2. Form that 5 records to a delimited string some thing like id1,name1,address1|id2,name2,address2 ..etc. 5以分隔的字符串记录一些格式,例如id1,name1,address1|id2,name2,address2 In the server side you can tokenize and store in the Oracle. 在服务器端,您可以标记化并存储在Oracle中。

JSON has standard format so that will be better option. JSON具有标准格式,因此这将是更好的选择。 The javascript that is used to store in HTML5 can be used to form this string in the client and once submit read from the client and push to the controller. 用于存储在HTML5中的javascript可用于在客户端中形成此字符串,并且一旦提交从客户端读取并推送到控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM