Grails 圣杯赢了';t使用get(params.id)从列表中检索单个项
我有一个表格,您可以在其中输入信息,然后显示信息,当您单击它的名称时,我希望它转到一个新页面,其中只显示单击的项目,而不是所有内容 这是我的控制器Grails 圣杯赢了';t使用get(params.id)从列表中检索单个项,grails,grails-controller,Grails,Grails Controller,我有一个表格,您可以在其中输入信息,然后显示信息,当您单击它的名称时,我希望它转到一个新页面,其中只显示单击的项目,而不是所有内容 这是我的控制器 class RecipeController { def index() { def recipe = Recipes.list() //Recipes is the Grails Domain [recipe: recipe] } def newRecipeForm() { } def createRecipe() {
class RecipeController {
def index() {
def recipe = Recipes.list() //Recipes is the Grails Domain
[recipe: recipe]
}
def newRecipeForm() {
}
def createRecipe() {
def r = new Recipes(name: params.name, course: params.course, diet: params.diet)
r.save()
redirect(action:"index")
}
def deleteRecipe() {
def r = Recipes.get(params.ID)
r.delete()
redirect(action:"index")
}
def showRecipe() {
def rec = Recipes.get(params.ID)
[recipe: rec]
}
}
My index.gsp,其中配方名称是可单击的,它应该通过ID重定向到一个新页面,在那里它只显示配方信息
<g:each var="Recipes" in="${recipe}">
<tbody>
<tr>
<td><g:link action="showRecipe" id="${Recipes.id}">${Recipes.name}</g:link></td>
</tr>
</tbody>
</g:each>
${Recipes.name}
最后是我的showRecipe.gsp,其中配方应该自己显示……但它一直显示我添加的所有配方
<g:each var="rec" in="${recipe}">
<tbody>
<tr>
<td>${rec.name}</td>
</tr>
</tbody>
</g:each>
${rec.name}
任何指导都会很棒!谢谢我可以说您的第一个错误是在索引中 您拥有的Recipe.id很可能正在检索所有id并在链接中发送它们。您不应该在属性名称中使用大写字母,编译器可能会将该属性视为一个类。代码应该更像:
${recipes.name}
在show()操作中添加println(params)或log.info(params),以打印所有参数并准确查看从视图接收到的内容
还要注意命名约定。您可能想更改recipeList或其他内容的配方,并将配方更改为recipeInstance或简单的配方。这将使代码更具可读性,并使我们更容易帮助您
编辑
正如@Nitin Dhomse所说的,您只需要访问单个配方的数据,因此不需要这样做
<g:each var="rec" in="${recipe}">
在你的节目里
更像是
<table>
<tbody>
<tr>
<td>${recipe?.id}</td>
<td>${recipe?.name}</td>
....
</tbody>
</table>
${recipe?.id}
${recipe?.name}
....
此外,如果在show.gsp中找不到recipe实例或访问$(recipe?.name)等属性,则应该在show()操作中重定向,否则将出现空指针异常。首先,我将使用
作为outter元素,其次,你能解释一下你的核心问题是什么吗?如果你只想要第一个值,为什么要使用。为什么要使用params.ID?应该是params.id。在createRecipe中,您也可以更改为新配方(参数)。请在show()操作中添加println(参数),并告诉我您看到了什么。
<table>
<tbody>
<tr>
<td>${recipe?.id}</td>
<td>${recipe?.name}</td>
....
</tbody>
</table>