2016-04-12 14 views
0

Grails 2.4.3を使用しています。すべてのドメインクラスで使用できるいくつかの標準メソッドを含む動的抽象ドメインクラスコントローラを作成しようとしています。動的抽象Grailsコントローラ

だから私は今、私はDomainClasscontrollerを拡張BarControllerをしたいDomainClassController

abstract class DomainClassController { 
    def domainClassSearchService 

def domainClass = Foo 
    ApplicationContext context = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT) as ApplicationContext 
    ConfigObject config = context.getBean(GrailsApplication).config 

    def index() { 

     if (!domainClass) 
      return render(text: 'fehler', status: INTERNAL_SERVER_ERROR) 
     def list = domainClassSearchService.list(params, domainClass, params.max, params.offset, params.sort, params.order) 
     Integer count = domainClassSearchService.count(params, domainClass) 
     render view: 'index', model: [list: list, count: count] 
    } 

    def search() { 

     if (!domainClass) 
      return render(text: 'fehler', status: INTERNAL_SERVER_ERROR) 

     def list = domainClassSearchService.list(params, domainClass, params.max, params.offset, params.sort, params.order) 
     Integer count = domainClassSearchService.count(params, domainClass) 

     render template: 'list', model: [list: list, count: count, params: params] 
    } 

} 

を作成しました:

私は抽象コントローラがためにそれを使用することができ、各コントローラにdomainClassを設定するにはどうすればよい
class BarController extends DomainClassController { 
    def domainClass = Bar 
} 

インデックスと検索方法?それが仕事を得るために答えで説明しているように私はそれをやった

EDIT

。 しかし、今、私は私がこれを追加してメソッドの動的に作成したい:

def create(){ 
    def domainClassObject = getDomainClass()?.newInstance() 
    domainClassObject.properties = params 

    return render(view: getViewPath() + 'create', model: [domainClass: domainClassObject]) 
} 

自体でも、この作品が、私はGSPプロパティdomainClassに使用したくありません。下のCasでクラス名を使用したいので、f.e.ビュー内のクラスBarの場合はクラスFooの場合はfoobarの場合

lowerCaseのClassNameにモデル名を設定するにはどうすればよいですか?

+1

プロパティの代わりに、抽象メソッドを使用できます。 –

+0

あなたの問題とは関係なく、 'ApplicationContext context = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)の代わりにApplicationContext'と' ConfigObject config = context.getBean(GrailsApplication).config'を使用すると、 'GrailsConfigurationAware'と'setConfiguration'メソッドで' config'プロパティを初期化します。あなたの例は 'config'を使用していないので、どうしてもそれが必要な理由が明確ではありません。 –

答えて

1

あなたは可能性がDこれはRestfulControllerと同じ方法です。 https://github.com/grails/grails-core/blob/v2.5.4/grails-plugin-rest/src/main/groovy/grails/rest/RestfulController.groovyを参照してください。

Classresourceという名前のプロパティは、https://github.com/grails/grails-core/blob/d45c00be6d8fdcce3edd21e16b50e30df9151b58/grails-plugin-rest/src/main/groovy/grails/rest/RestfulController.groovy#L37で定義されています。 newInstance()メソッドが呼び出され、新しいインスタンスが作成されます。Classhttps://github.com/grails/grails-core/blob/d45c00be6d8fdcce3edd21e16b50e30df9151b58/grails-plugin-rest/src/main/groovy/grails/rest/RestfulController.groovy#L267を参照してください。

class RestfulController<T> { 

    Class<T> resource 
    String resourceName 
    String resourceClassName 
    boolean readOnly 

    // ... 

    RestfulController(Class<T> resource) { 
     this(resource, false) 
    } 

    RestfulController(Class<T> resource, boolean readOnly) { 
     this.resource = resource 
     this.readOnly = readOnly 
     resourceClassName = resource.simpleName 
     resourceName = GrailsNameUtils.getPropertyName(resource) 
    } 

    // ... 

    /** 
    * Lists all resources up to the given maximum 
    * 
    * @param max The maximum 
    * @return A list of resources 
    */ 
    def index(Integer max) { 
     params.max = Math.min(max ?: 10, 100) 
     respond listAllResources(params), model: [("${resourceName}Count".toString()): countResources()] 
    } 

    /** 
    * Creates a new instance of the resource. If the request 
    * contains a body the body will be parsed and used to 
    * initialize the new instance, otherwise request parameters 
    * will be used to initialized the new instance. 
    * 
    * @return The resource instance 
    */ 
    protected T createResource() { 
     T instance = resource.newInstance() 
     bindData instance, getObjectToBind() 
     instance 
    } 

    /** 
    * List all of resource based on parameters 
    * 
    * @return List of resources or empty if it doesn't exist 
    */ 
    protected List<T> listAllResources(Map params) { 
     resource.list(params) 
    } 

    /** 
    * Counts all of resources 
    * 
    * @return List of resources or empty if it doesn't exist 
    */ 
    protected Integer countResources() { 
     resource.count() 
    } 
} 
1

あなたは、各コントローラ(サブクラス)でドメインクラスを設定し、抽象メソッドとしてそれを実装することにより、抽象クラスにそれがアクセスできるようにすることができ:

DomainClassController.groovy

abstract class DomainClassController { 
    def domainClassSearchService 

    ApplicationContext context = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT) as ApplicationContext 
    ConfigObject config = context.getBean(GrailsApplication).config 

    abstract Class getDomainClass() 

    def index() { 

     if (!domainClass) 
      return render(text: 'fehler', status: INTERNAL_SERVER_ERROR) 
     def list = domainClassSearchService.list(params, domainClass, params.max, params.offset, params.sort, params.order) 
     Integer count = domainClassSearchService.count(params, domainClass) 
     render view: 'index', model: [list: list, count: count] 
    } 

    def search() { 

     if (!domainClass) 
      return render(text: 'fehler', status: INTERNAL_SERVER_ERROR) 

     def list = domainClassSearchService.list(params, domainClass, params.max, params.offset, params.sort, params.order) 
     Integer count = domainClassSearchService.count(params, domainClass) 

     render template: 'list', model: [list: list, count: count, params: params] 
    } 

} 

BarController.groovy

class BarController extends DomainClassController { 
    Class getDomainClass() { 
     Bar  
    } 
} 
+0

これはうまく働いています。 – YAT

関連する問題