Object in Spring form

0

I have a form in which I have some input in which when filling in with js I pick them up and add them to a hidden input that I have on the page.

document.editRouteForm.itinerary.value = itiToObj();
document.editRouteForm.itinerary.value = JSON.stringify(itiToObj());

The first one adds this information to me in the field:

And the second this:

Once it arrives at the controller it gives me a bug the BindingResult that tells me the following (this is if I do JSON.stringfy):

org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'route' on field 'itinerary': rejected value [{"name":"ch","color":"chjjhfd","salidasEntradas":["1","2","3"]}]; codes [typeMismatch.route.itinerary,typeMismatch.itinerary,typeMismatch.es.ticnor.trayecbus.model.Itinerary,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [route.itinerary,itinerary]; arguments []; default message [itinerary]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'es.ticnor.trayecbus.model.Itinerary' for property 'itinerary'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'es.ticnor.trayecbus.model.Itinerary' for property 'itinerary': no matching editors or conversion strategy found]

(this if I return the object):

Field error in object 'route' on field 'itinerary': rejected value [[object Object]]; codes [typeMismatch.route.itinerary,typeMismatch.itinerary,typeMismatch.es.ticnor.trayecbus.model.Itinerary,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [route.itinerary,itinerary]; arguments []; default message [itinerary]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'es.ticnor.trayecbus.model.Itinerary' for property 'itinerary'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'es.ticnor.trayecbus.model.Itinerary' for property 'itinerary': no matching editors or conversion strategy found]

Then with this error I make sure that the information arrives at the backend but is not able to process it.

This is the object I'm working with:

public class Itinerary implements Serializable {

 private String name;
 private String[] salidasEntradas;
 private String color;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String[] getSalidasEntradas() {
  return salidasEntradas;
 }

 public void setSalidasEntradas(String[] salidasEntradas) {
  this.salidasEntradas = salidasEntradas;
 }

 public String getColor() {
  return color;
 }

 public void setColor(String color) {
  this.color = color;
 }

 @Override
 public String toString() {
  return "Itinerary{" + "name=" + name + ", salidasEntradas=" + salidasEntradas + ", color=" + color + '}';
 }
}
    
asked by Eduardo 18.05.2018 в 12:32
source

1 answer

1

The two scenarios you have are the following:

  • You try to put a JS object as the value of a form. It does not work because all the values must be a string. And any object is transformed to string in that way, setting [object Object] .
  • You make a JSON.stringify of the object: You get a string.
  • In both cases the same thing happens: Spring receives a field with String , but you have defined that it should be a Itinerary object.

    I see two solutions:

  • Add to your form:

    • A hidden input with name="itinerary.name"
    • A hidden input with name="itinerary.color"
    • A hidden input with name="itinerary.salidasEntradas" for each output value Entries you have .
  • Modify your Route object so that the Itinerary setter supports a String and processes it as JSON, generating the Itinerary

  • answered by 18.05.2018 / 13:04
    source