Spring jsp binding bean binding form

I tried to link POJO using a tag library spring form. After binding the reference classes to bean variables, I get "Error request error - status 400".

If I unlink the reference class, my form submits successfully, and the 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 links from several articles , but they load a list of bean links on get request, while I add lines before submitting JSP.

enter image description here

+4
source share
3 answers

I found that the problem is increasing the index after clicking "Add Row". Therefore, I added the following jQuery code to increase the 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>

POJO. Multipart ( TourDetailsPojo Bean)

private Date departDate;

private Multipart tourTicket;

//getters and setters
0

JSTL .

<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>

taglib JSP.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
0

,

  • <tr>
  • html, , strong > → HTML.
    , HTML

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

    for something like this

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

  • Create a html generated template

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

    See replacement 0for{INDEX}

  • On Add Rowclick button, select lengthfrom <tr>, replace {INDEX}with length using javascript function
    generated_template = template.replace(/{INDEX}/g,length);

  • Add generated_templateto table.

Hope this helps ...

0
source

Source: https://habr.com/ru/post/1692913/


All Articles