如何在2个以上的域中使用Grails可搜索插件

如何在2个以上的域中使用Grails可搜索插件,grails,grails-plugin,grails-2.0,grails-domain-class,Grails,Grails Plugin,Grails 2.0,Grails Domain Class,我对Grails一无所知,从过去几天开始学习Grails。我试图通过在我的演示grails应用程序中使用来添加搜索功能。我成功地添加了用户搜索,用户可以搜索其他用户并跟踪他们。我正在这样做 grails install-plugin searchable 域人.groovy-- 这是表单视图 view/post/list.gsp-- ----一些代码----- ------一些代码------ 现在,当我尝试按帖子标题搜索帖子时,它会显示错误。它覆盖了可搜索的操作。如何解决此问题?您可以使用

我对Grails一无所知,从过去几天开始学习Grails。我试图通过在我的演示grails应用程序中使用来添加搜索功能。我成功地添加了用户搜索,用户可以搜索其他用户并跟踪他们。我正在这样做

grails install-plugin searchable
域人.groovy--

这是表单视图

view/post/list.gsp--

----一些代码-----
------一些代码------

现在,当我尝试按帖子标题搜索帖子时,它会显示错误。它覆盖了可搜索的操作。如何解决此问题?

您可以使用searchable实现自己的搜索方法,从搜索表单调用控制器函数,并在以下位置执行搜索:

假设您有两个搜索表单:

<g:form controller="postsController" action="postAction" class="navbar-search pull-left">
   <g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
 </g:form>
同样,您可以为另一个搜索使用不同的函数,如果您使用远程表单,那么您需要在搜索页面上有两个不同的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>

搜索栏:
--您对表单的搜索结果将显示在此处--
此远程表单将在控制器中调用postAction方法,您可以在控制器的视图文件夹中使用postAction.gsp页面并将结果打印出来

在您的搜索页面上,postSearchResultsDiv将显示搜索结果(postAction GSP页面输出)

我解决了自己的问题。。。 我是这样做的

grails install-plugin searchable
后置控制器---

搜索表---


searchpost.gsp//用于显示结果

<html>
   <head>
    <r:require modules="bootstrap"/>
    <meta name="layout" content="main"/>        
   </head>
   <body>
     <g:render template="/layouts/header" />    
     <div class="well"> 
        <g:each var="post" in="${searchResult?.results}">
          <div>
           <h2>${post.title}</h2>
           <p>${post.teaser}</p>
           <p>Last Updated: ${post.lastUpdated}</p>
           <g:link controller="post" action="view" id="${post.id}" class="btn btn-success">
         View this post
          </g:link>
          <g:if test="${post.author == currentLoggedInUser }">
         <g:link controller="post" action="edit" id="${post.id}" class="btn btn-danger">
             Edit this post
         </g:link>
         <g:actionSubmit action="delete" value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" class="btn btn-inverse" />
        </g:if>
        <g:form>
     <g:hiddenField name="id" value="${post?.id}" />            
        </g:form>
   </div>

   </g:each>
  </div>        
 </body>
 </html>

${post.title}
${post.trister}

上次更新:${post.lastUpdated}

查看此帖子 编辑这篇文章

而且很有效:)

任何人都可以帮我吗??若你们不明白这个问题,那个就告诉我。我会改进它…你在搜索时出现了什么错误,因为searchable看起来不错,你的控制器中的代码是什么?你的searchable没有显示任何错误。我工作正常,但两个搜索文本字段只执行一个搜索。在我的个人域中,我使用searchable搜索其他用户。现在,我也在使用相同的搜索功能来搜索帖子域,而不是显示帖子条目,这是一个不正确的人物搜索。如何区分这两种搜索,以便在不同的域上执行不同的操作。我使用相同的index.gsp页面来显示搜索结果。如您在view/searchable/index.gsp中所见。如何在不同视图上呈现后期搜索结果。请参见下面的答案,您可以在控制器中使用不同的-2搜索功能,并相应地呈现搜索视图
<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>
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()]
        }
<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>
import org.compass.core.engine.SearchEngineQueryParseException
class PostController 
{
   def searchableService

   def searchpost = {
    if (!params.q?.trim()) {
        return [:]
    }
    try {
        return [searchResult: searchableService.search(params.q, params)]
    } catch (SearchEngineQueryParseException ex) {
        return [parseException: true]
    }
    render(view:'searchpost')
   }

  .......
}   
<g:form controller="post" action="searchpost" class="navbar-search pull-left">
   <g:textField name="q" value="" class="search-query" placeholder="Search Posts"/>
 </g:form>
<html>
   <head>
    <r:require modules="bootstrap"/>
    <meta name="layout" content="main"/>        
   </head>
   <body>
     <g:render template="/layouts/header" />    
     <div class="well"> 
        <g:each var="post" in="${searchResult?.results}">
          <div>
           <h2>${post.title}</h2>
           <p>${post.teaser}</p>
           <p>Last Updated: ${post.lastUpdated}</p>
           <g:link controller="post" action="view" id="${post.id}" class="btn btn-success">
         View this post
          </g:link>
          <g:if test="${post.author == currentLoggedInUser }">
         <g:link controller="post" action="edit" id="${post.id}" class="btn btn-danger">
             Edit this post
         </g:link>
         <g:actionSubmit action="delete" value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" class="btn btn-inverse" />
        </g:if>
        <g:form>
     <g:hiddenField name="id" value="${post?.id}" />            
        </g:form>
   </div>

   </g:each>
  </div>        
 </body>
 </html>