我在我的應用程序中聲明瞭兩個域。Grails - 多對多搜索
class Posts {
String title
String content
static hasMany = [tags:Tag]
static constraints = {
}
}
class Tag {
String Name
static belongsTo = Post
static hasMany = [posts:Post]
static constraints = {
}
String toString()
{
"Tag:${Name}"
}
}
我有一個控制器,其管理結果的搜索和顯示:
package com.trafigura.com.trafigura
class ViewerController {
def defaultAction='search'
def search={}
def show = {
def _foundPost = Post.findAllBytitle(params.title)
return [posts: _foundPost, term: params.title]
}
}
的search.gsp中代碼如下:
<html>
<head><title>Simple GSP page</title></head>
<body>Place your content here.
<formset>
<legend>TagsPosts</legend>
<g:form action="show">
<label for="title">Title</label>
<g:textField name="title" />
<g:submitButton name="search" value="Search"/>
</g:form>
</formset>
</body>
</html>
和用於顯示下面的代碼。 GSP。
<html>
<head><title>Simple GSP page</title></head>
<body><h1>Results</h1>
for items matching <em>${term}</em>.
Found <strong>${posts.size()}</strong> hits.
</p>
<ul>
<g:each var="tag" in="${posts.tags}">
<li>${tag.Name}</li>
</g:each>
</ul>
<g:link action='search'>Search Again</g:link></body>
</html>
我的問題是我無法顯示標籤如下:
結果
找到1次命中。
* [planting, dig]
不過,我想輸出爲:我在做什麼錯在這裏
* planting
* dig
?
備受讚賞。
我注意到你的代碼'Post.findAllBytitle'使用它小寫的 '標題' - 是造成你的任何錯誤?我本來以爲它是'Post.findAllByTitle',大寫'T'。 – 2010-03-10 16:19:09
感謝羅布是的,我確實在那裏犯了一個錯字,但是我確實找到了解決問題的辦法。
def postfound = Post.findByTitle(params.title)
由於findAllByTitle的問題在於它返回了Phillpe在他的答案中正確指定的可能值的列表。 感謝回覆, 乾杯 WB – WaZ 2010-03-10 17:11:40