Transferring data from the GSP to the controller in Grails

I am creating a GSP page with controls depending on the rows in the database.
It depends on the value returned. <g:each in="${Vehicles}" var="vehicle"> So, if there are 3 machines, 3 lines with text fields will be created. (Maximum may be 200)

<g:form action="update" >
      <label for="SearchTerm">${term}</label>

          <g:each in="${Vehicles}" var="vehicle">
            <tr>
                     <td> <label for="Name">${vehicle.name}</label> </td>
                     <td><g:textField name="${vehicle.id}.ModelNo" /> </td>
                     <td><g:textField name="${vehicle.id}.Year" /> </td>
            </tr>
          </g:each>
  <td> <g:submitButton name="update" value="Update"/></td>
  </g:form>

How can I pass this value to my controller in order to then save or update data in the database. or is there an easy way to achieve this scenario?

+3
source share
2 answers

You need this code in GSP

    <g:form action="update" >
          <label for="SearchTerm">${term}</label>

              <g:each in="${Vehicles}" var="vehicle" status="i">
                <tr>
                         <td> <label for="Name">${vehicle.name}</label> </td>
                         <td><g:hiddenField name="vehicle[${i}].id" value="${vehicle.id}"/>
<g:textField name="vehicle[${i}].ModelNo" value="${vehicle.ModelNo}"/> </td>
                         <td><g:textField name="vehicle[${i}].Year" value="${vehicle.Year}"/> </td>
                </tr>
              </g:each>
      <td> <g:submitButton name="update" value="Update"/></td>
    </g:form>

The controller must either have a domain with the List property, or a Command object with the List property, i.e.

SearchCommand {   
  List<Vehicle> vehicle = new Arraylist<Vehicle>(3); 
}

Then in the controller (if the command object is used)

def save = {SearchCommand searchCmd-> 
  searchCmd.vehicle.each {vehicle ->
     /* Process Vehicle */
  }
}

Hope that helps

+6
source

. , , :

idList.each {
theYear=request.getParameter(it+Year)
}

, -

java.util.Enumeration theFields=request.getParameterNames()
theFields.each {
//look at your field name and take appropriate action
}

. this

+2

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


All Articles