我有一个表格,您可以在其中输入信息,然后显示该信息,当您单击它的名称时,我希望它转到一个新页面,其中仅显示单击的项目而不是所有内容。
这是我的控制器...
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]
}
}
我的 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>
最后是我的 showRecipe.gsp,食谱应该单独显示......但它一直显示我添加的所有内容
<g:each var="rec" in="${recipe}">
<tbody>
<tr>
<td>${rec.name}</td>
</tr>
</tbody>
</g:each>
任何指导都会很棒! 谢谢
我可以说你的第一个错误在你的索引中。
您拥有的 Recipe.id 很可能是检索所有 id 并在链接中发送它们。 不应在属性名称中使用大写,编译器可能会将该属性视为类。代码应该更像:
<tr>
<td><g:link action="showRecipe" id="${recipes.id}">${recipes.name}</g:link></td>
</tr>
</g:each>
在 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>
此外,如果您找不到配方实例,则应在 show(( 操作中重定向,或者访问您的属性,如 $(recipe?(。name( 在你的 show.gsp 中,否则你会得到 nullPointer 异常。