【问题标题】:How to use Grails Searchable plugins in more than 2 domains如何在超过 2 个域中使用 Grails Searchable 插件
【发布时间】:2013-07-08 07:28:09
【问题描述】:

我是 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)
}
}

这是表单视图

查看/发布/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 ------

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

【问题讨论】:

  • 谁能帮帮我??如果您不明白问题,请告诉我。我会改进它...
  • 您在搜索时遇到什么错误,因为可搜索看起来不错,您的控制器中的代码是什么?
  • Ya Searchable 没有显示任何错误。我工作正常,但两个搜索文本字段只执行一个搜索。在我的个人域中,我使用可搜索来搜索其他用户。现在,我也用于 Post 域的相同搜索,而不是显示帖子项目,它作为不正确的人员搜索工作。我如何区分这两个可搜索的,以便我可以在不同的域上执行不同的操作。
  • 我使用相同的 index.gsp 页面进行两个搜索以显示搜索结果。正如您在 view/sea​​rchable/index.gsp 中看到的那样。如何在不同的视图上呈现帖子搜索结果..
  • 看下面的答案,你可以在控制器中拥有不同的2个搜索功能,并相应地渲染搜索视图

标签: grails grails-plugin grails-2.0 grails-domain-class


【解决方案1】:

您可以使用 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>

 <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>

此远程表单将调用控制器中的 postAction 方法,您可以在控制器的视图文件夹中拥有 postAction.gsp 页面并将结果打印出来。

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

【讨论】:

    【解决方案2】:

    我解决了我自己的问题... 我已经这样做了..

    后控制器---

    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>
    

    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>
    

    它有效:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多