There is a jsp which outputs a HTML5 page. The HTML5 has two buttons – “Add” and “Save”. HTML5 local storage feature is used to store offline data.
“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. When “Add” button is clicked, a javascript is run which adds the record to a result set. So for 5 clicks the javascript result set contains 5 records.
“Save” button, when clicked, must insert all the 5 records from the javascript result set to the Oracle database. To do this the list of records from result set must be passed to the Spring controller.
The Spring controller has been coded using 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?
I have couple of thoughts
Form that 5 records into a json string and post to the controller and unmarshall the json to object and store in Oracle.
Form that 5 records to a delimited string some thing like
id1,name1,address1|id2,name2,address2..etc. In the server side you can tokenize and store in the Oracle.JSON has standard format so that will be better option. 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.