我是Grails的新手,从过去的几天开始学习grails。我正在尝试在我的演示grails应用程序中使用searchable plugin添加搜索功能。我在用户搜索中成功添加了searchable plugins,用户可以在其中搜索其他用户并关注他们。我是这样
grails install-plugin searchable
域Person.groovy-
package org.grails.twitter
class Person {
transient springSecurityService
String realName
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static hasMany = [followed:Person, status:Status]
static searchable = [only: 'realName']
static constraints = {
username blank: false, unique: true
password blank: false
}
static mapping = {
password column: '`password`'
}
Set<Authority> getAuthorities() {
PersonAuthority.findAllByPerson(this).collect { it.authority } as Set
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
查看/搜索/index.gsp-
<html>
<head>
<meta name="layout" content="main" />
<title>What Are You Doing?</title>
<g:javascript library="jquery" plugin="jquery" />
</head>
<body>
<h1>Search For People To Follow</h1>
<div class="searchForm">
<g:form controller="searchable">
<g:textField name="q" value=""/>
</g:form>
</div>
<h1>What Are You Doing?</h1>
<div class="updateStatusForm">
<g:formRemote onSuccess="document.getElementById('messageArea').value='';" url="[action: 'updateStatus']" update="messageLists" name="updateStatusForm">
<g:textArea name="message" value="" id="messageArea" /><br/>
<g:submitButton name="Update Status" />
</g:formRemote>
</div>
<div id="messageLists">
<g:render template="messages" collection="${messages}" var="message"/>
</div>
</body>
</html>
它工作正常。现在我的问题开始了。现在,我想在我的帖子域中添加此可搜索项,用户也可以在其中搜索帖子项。我正在这样...
域Post.groovy-
package groovypublish
class Post {
static hasMany = [comments:Comment]
String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
static searchable = [only: 'title']
static constraints = {
title(nullable:false, blank:false, length:1..50)
teaser(length:0..100)
content(nullable:false, blank:false)
lastUpdated(nullable:true)
published(nullable:false)
}
}
这是表单 View
查看/发布/list.gsp-
------ some code -----
<g:form controller="searchable" class="navbar-search pull-left">
<g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
</g:form>
------ some code ------
现在,当我尝试按帖子标题搜索帖子时,它显示错误。它会覆盖可搜索的操作。如何解决这个问题呢 ?
最佳答案
您可以使用searchable实现自己的搜索方法,从搜索表单中调用 Controller 函数,然后执行搜索:
假设您有两种搜索形式:
<g:form controller="postsController" action="postAction" class="navbar-search pull-left">
<g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
</g:form>
和
<g:form controller="searchable">
<g:textField name="q" value=""/>
</g:form>
然后在PostCOntroller中,您可以使用postAction方法来执行搜索:
def postAction (Integer max) {
params.max = Math.min(params.max ? params.int('max') : 10, 100)
params.sort = "id"
params.order = "desc"
if(params?.q){
def result = Post .search(params.q , order:"desc" )
return [searchResults: result.results, searchResultsCount: result.total, popup : params.popup?.toBoolean()]
}else{
[searchResults: Post .list(params), searchResultsCount: Post .count(), popup : params.popup?.toBoolean()]
}
同样,您可以为另一个搜索使用不同的功能,如果您使用远程表单,则需要在搜索页面上具有两个不同的div,然后可以在其中呈现结果页面。
假设您有:
<g:formRemote name="postSearchForm" update="postSearchResultsDiv" url="[controller: 'post', action:'postAction' , params: [popup: false]]">
<label for="searchText">Search Post:</label>
<input name="q" type="text" id="searchText" class="input-medium search-query"/>
<input id="searchButton" type="submit" class="btn-info" value="Search"/>
</g:formRemote>
<div id="postSearchResultsDiv">--Your search result for the form will display here--</div>
此远程表单将在您的 Controller 中调用 postAction 方法,您可以在 Controller 的view文件夹上创建 postAction.gsp 页面,并在那里打印结果。
在搜索页面上, postSearchResultsDiv 将具有搜索结果(postAction GSP页面输出)
关于grails - 如何在超过2个域中使用Grails可搜索插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17521136/