How to get the value of the attributes of a form with spring?

1

I have a form, in which I want to retrieve the values entered in the fields, I have the following code in the controller

@RequestMapping(value= "/views/consulta", method = RequestMethod.GET)
public String redireccionaConsultaPage(Locale locale, Model model) {
        model.addAttribute(Constantes.CONSULTA_FORM, new Consulta());
        return Constantes.CONSULTA_VISTA;
}

I made a Query class, which contains the attributes that I retrieve from the form, including name and payment, and payment. and pass it as a parameter to the model.addAttribute (), I also pass a string called "query" which is the name I put in the modelAttribute attribute of my form, only that I put it in a constant, only I do not understand even what is that for? Now, if I want to paint in console that data that I recover from the form, how do I do it? Would I have to do another method? is that I want to see that data in the console and also since I have those attributes, I want to pass them as a parameter to a method to make a query and filter with that data I just start with spring and I do not know very well How do I recover that data?

this is my form

<form: form id="consulta" modelAttribute="consulta">
  <tr>
    <td><label>CENTRO PAGO</label></td>
    <td><form: input type="text" id="pago" path="nombrePago"/></td>
   </tr>
   
   <tr>
    <td><label>DIRECC PAGO</label></td>
    <td><form: input type="text" id="direccpago" path="direccPago"/></td>
   </tr>


</form>
    
asked by Root93 30.05.2018 в 06:17
source

1 answer

3

The modelAttribute is a property that references your object within the view to the controller. On the other hand, in the code model.addAttribute(Constantes.CONSULTA_FORM, new Consulta()); , in the first parameter you pass the AttirbuteName assigned in your JSP that is to say this part of the beginning of your form modelAttribute="consulta" , and in the second parameter you pass the object or AttributeValue that will contain the data that you will pass through your form (it should be clarified that it also has the functionality of passing data to you and reproducing them in your form). So to make it clearer your controller method would look like this:

@RequestMapping(value= "/views/consulta", method = RequestMethod.GET)
public String redireccionaConsultaPage(Consulta consulta, Locale locale, Model model) {
        model.addAttribute("consulta", consulta);
        System.out.println(consulta.toString()); //Esta sería una forma de mostrar por consola. Puedes manipulas los datos con los get de tu Clase.
        return Constantes.CONSULTA_VISTA;
}
  

I leave this link, from SO where they explain about this property:    link

To finish it I do not know if you already researched it but I want to clarify this parameter in the JSP, in the part of the input, the parameter path will refer to the variables of your object, that is, in this case to the variables created in the Query class.

<form:form method="GET" id="consulta" modelAttribute="consulta">
  <tr>
    <td> <label>CENTRO PAGO</label> </td>
    <td> <form:input type="text" id="pago" path="nombrePago" /> </td>
   </tr>

   <tr>
    <td> <label>DIRECC PAGO</label></td>
    <td> <form:input type="text" id="direccpago" path="direccPago" /> </td>
   </tr>
</form:form> <%-- Falto cerrar el formulario --%>
  

I also leave you the Spring Framework link Reference Documentation   4.3.18.BUILD-SNAPSHOT:    link

I hope I help you.

    
answered by 30.05.2018 / 08:19
source