retrieve form data with spring?

0

I have this method in the controller that redirects me to the page where the form is

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

in the model addAtribute, I pass the object that I want to fill with the data of the form, but how do I pull that data from the object ?, I change it in the following way

@RequestMapping(value= "/views/consulta", method = RequestMethod.GET)
public String redireccionaConsultaPage(Consulta consul, Locale locale, Model model) {
        model.addAttribute("consultaForm", consul);
        system.out.println("prueba" +consul.getClavePago()) 
        return Constantes.CONSULTA_VISTA;
}

What I want to do is that by filling out the form and clicking on the search button, I get those form values and see them in the console and I put a system to see if it painted a field of the form, but it takes it out, and that is when the page starts in automatic enters that method and puts it null, and not even put value to the input of the form, as I can do so that I will not take it later after starting the page, but until I put a value and click on search

this is my signature

<form: form id="consulta" modelAttribute="consultaForm">
  <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>
    <input type="submit" />
   </tr>


</form>

what is inside of path, are the attributes that I have in my object What is the best way to obtain this data? I saw that it could be with RequestParam, I think but I do not know how to use it help

    
asked by Root93 05.06.2018 в 05:48
source

2 answers

1

You must add in the controller a method that knows how to manage the POST of that form.

In the HTML code, define the action (the route to which you will send data) for clarity:

<form:form id="consulta" action="/doconsulta" modelAttribute="consultaForm">
    <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>
        <input type="submit" />
    </tr>
</form:form>

And in the controller, you add the following method:

@RequestMapping(value = "/doconsulta", method = RequestMethod.POST)
public String ejecutaConsulta(@ModelAttribute("consultaForm") Consulta consulta, BindingResult result, Model model) {
    //...
}

Assuming that those payment and paymentaddress that you have defined in the form are attributes of the Query class.

The first method that you have defined, for the GET, is already fine, you do not need to do what you say next.

Greetings

    
answered by 05.06.2018 / 09:32
source
0

You must change this piece:

@RequestMapping(value= "/views/consulta", method = RequestMethod.POST)
public String redireccionaConsultaPage(Consulta consul, Locale locale, Model model) {
        model.addAttribute("consultaForm", consul);
        system.out.println("prueba" +consul.getClavePago()) 
        return Constantes.CONSULTA_VISTA;
}

In automatic it will pick up the name of the form that match your object consul of the class Consulta (Note that it changes from GET to POST ). You can use serialize with load of JQuery to send a post without reloading the page.

The first controller is fine. What it does is create new Consulta() to pass it to the form.

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

As a main data

    
answered by 05.06.2018 в 16:47