4

I was trying to bind POJO using spring form tag library. After binding reference class bean variables, I am getting "Bad Request Error- Http Status 400".

If I remove binding of reference class, My form is submitted successfully and values are also populated inside the class.

public class EmployeeTourPojo {

  private String empDisplayName;

  private List<TourDetailsPojo> tourDetails;

  //getter and setter

}

and

public class TourDetailsPojo {

    private Date departDate;

    private String departTime;

//getters and setters

}

JSP:

<td><form:input path="empDisplayName" class="form-control"/>    </td>

<form:input placeholder="Departure Date" path="tourDetails[${index}].departDate" required="required" class="datepicker form-control"/><br/>

I had got references from few articles but they are loading the list of reference bean at the get request while I am adding the rows before the submitting the JSP.

enter image description here

Ankit
  • 2,126
  • 4
  • 33
  • 53

3 Answers3

0

Use JSTL tag library to iterate data from list.

<c:forEach var="tourDetails" items="${tourDetails}" varStatus="status">
<form:input placeholder="Departure Date" path="tourDetails[${status.index}].departDate" required="required" class="datepicker form-control"/><br/>
</c:forEach>

Also add below taglib at starting of your JSP.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  • as I mentioned, my input div is generated dynamically (using add row on jsp). So I cannot use for loop to bind input variable. – Ankit Feb 09 '18 at 05:11
0

If I understood your problem correctly this will help you

  1. Inspect your table row <tr> tag in browser
  2. Copy the html element, to make it as template, in inspected element Right Click and Copy -> Outer HTML.
    Suppose Generated HTML element looks like as below

    this will convert

    ...<form:input placeholder="Departure Date" path="tourDetails[${index}].departDate" required="required" class="datepicker form-control"/><br/>...

    to something like this

    ...<input placeholder="Departure Date" id="tourDetails0.departDate" name="tourDetails[0].departDate" required="required" class="datepicker form-control"/><br/>...

  3. Create template of generated html

    var template = '...<input placeholder="Departure Date" id="tourDetails{INDEX}.departDate" name="tourDetails[{INDEX}].departDate" required="required" class="datepicker form-control"/><br/>...';

    See I have replace 0 with {INDEX}

  4. On Add Row button click, get length of <tr>, replace {INDEX} with length, using javascript function
    generated_template = template.replace(/{INDEX}/g,length);

  5. Append generated_template to table.

Hope this will help...

Shantaram Tupe
  • 1,646
  • 3
  • 16
  • 42
0

I found the issue lies in incrementing the index after Clicking "Add Row". Hence, I had added following JQuery code to increment index.

$("#addRow").click(function(){
                    //alert("The button was clicked.");
                    var addressRow = $('.repeat-address').last();
                    var addressRowLength = $('.repeat-address').length;

                    var newAddressRow = addressRow.clone(true).find("input").val("").end(); 

                    $(newAddressRow).find("td input,td select").each(function(index,item) {
                        item.name = item.name.replace(/[0-9]/g,addressRowLength);
                    });

                    newAddressRow.insertAfter(addressRow);


                }); 

HTML CODE

<c:set var="index" value="0"/>
                    <td><form:input path="empDisplayName" class="form-control"/>    </td>
                    <tr class="repeat-address">

                        <td>Departure Date</td>
                        <td><form:input placeholder="Departure Date" path="tourDetails[${index}].departDate" required="required" class="datepicker form-control"/></td>


                        <td>Pincode</td>
                        <td><input type="text" name="tourDetails[${index}].departDate"/></td>
                        <td><input type="file" name="tourDetails[${index}].tourTicket"/></td>
                    </tr>
                    <tr><td colspan="3"><input id="addRow" type="button" value="Add Row"></td></tr>

Since, I also need to bind input type file with POJO. I had also Added one Multipart field (in TourDetailsPojo Bean) as

private Date departDate;

private Multipart tourTicket;

//getters and setters
Ankit
  • 2,126
  • 4
  • 33
  • 53