0

私は Grails の初心者で、ここ数日から Grails を学び始めました。デモ grails アプリケーションで検索可能なプラグインを使用して検索機能を追加しようとしています。ユーザー検索に検索可能なプラグインを追加し、ユーザーが他のユーザーを検索してフォローできるようにしました。私はこのようにやっています..

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

投稿のタイトルで投稿を検索しようとすると、エラーが表示されます。検索可能なアクションをオーバーライドします。この問題を解決するには?

4

2 に答える 2

1

searchable を使用して独自の検索メソッドを実装し、検索フォームからコントローラー関数を呼び出して、その中で検索を実行できます。

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>

次に、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()]
        }

リモートフォームを使用する場合は、検索ページに2つの異なる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 ページ出力) が表示されます。

于 2013-07-08T18:28:48.730 に答える
0

私は自分の問題を解決しました...私はこのようにしました..

ポストコントローラー ---

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>

そしてそれは動作します:)

于 2013-07-09T05:55:01.397 に答える