问题描述
创建一个带有控件的GSP页面,具体取决于数据库中的行。
这取决于由< g:each in =$ {Vehicles }var =vehicle>
因此,如果有3辆车,则会生成3行文本框。 (最多可以是200)
< g:form action =update>
< label for =SearchTerm> $ {term}< / label>
< 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:每个>
< td> < g:submitButton name =updatevalue =Update/>< / td>
< / g:表格>
我该如何将这个值传递给我的控制器,然后我可以保存/更新数据到数据库。或者有没有简单的方法来实现这种情况?
您需要在GSP中使用这样的代码
< g:form action =update>
< label for =SearchTerm> $ {term}< / label>
< tr>
< td> < label for =Name> $ {vehicle.name}< / label> < / TD>
< td>< g:hiddenField name =vehicle [$ {i}] .idvalue =$ {vehicle.id}/>
< g:textField name =vehicle [$ {i}]。ModelNovalue =$ {vehicle.ModelNo}/> < / TD>
< td>< g:textField name =vehicle [$ {i}]。年value =$ {vehicle.Year}/> < / TD>
< / tr>
< / g:每个>
< td> < g:submitButton name =updatevalue =Update/>< / td>
< / g:表格>
控制器需要有一个具有列表属性的域或具有列表属性的命令对象
SearchCommand {
List< Vehicle> vehicle = new Arraylist< Vehicle>(3);
$ / code>
然后在控制器中(如果使用命令对象)
def save = {SearchCommand searchCmd->
searchCmd.vehicle.each {vehicle - >
/ * Process Vehicle * /
}
}
帮助
I create a GSP page with controls depending on the rows in a database.
This depends on the value returned by the <g:each in="${Vehicles}" var="vehicle">
So, if there are 3 vehicles, 3 rows with text boxes will be generated. (The maximum can 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 basically pass this value to my controller so that I can then save/update the data to the database. or Is there any easy way to achieve this scenario?
You need some code like this in the 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 needs to either have a Domain with a List Property or a Command Object with a List Property ie
SearchCommand {
List<Vehicle> vehicle = new Arraylist<Vehicle>(3);
}
Then in the controller (if using the command object)
def save = {SearchCommand searchCmd->
searchCmd.vehicle.each {vehicle ->
/* Process Vehicle */
}
}
Hope that Helps
这篇关于将数据从GSP传递给Grails中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!