grails - 动态抽象Grails Controller

标签 grails

我正在使用Grails 2.4.3,并且试图创建一个动态的抽象域类 Controller ,其中包含一些标准方法,每个域类都可以使用该方法。

所以我创建了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]
    }

}

现在我想要一个扩展BarControllerDomainClasscontroller:
class BarController extends DomainClassController {
    def domainClass = Bar
}

如何在每个Controller中设置domainClass,抽象 Controller 可以将domainClass用于索引和搜索方法?

编辑

我做到了,就像答案中描述的那样使它起作用。
但是现在我想使create Method动态化,所以我添加了以下内容:
def create(){
    def domainClassObject = getDomainClass()?.newInstance()
    domainClassObject.properties = params

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

这项工作本身也可以,但是我不想在GSP中使用domainClass属性。我想在Lower Cas中使用Class名称,这样 View 中的类fooFoo和类barBar

如何在lowerCase中将模型名称设置为ClassName?

最佳答案

您可以像RestfulController一样执行此操作。参见https://github.com/grails/grails-core/blob/v2.5.4/grails-plugin-rest/src/main/groovy/grails/rest/RestfulController.groovy

https://github.com/grails/grails-core/blob/d45c00be6d8fdcce3edd21e16b50e30df9151b58/grails-plugin-rest/src/main/groovy/grails/rest/RestfulController.groovy#L37定义了一个名为Classresource属性。在该newInstance()上调用Class方法以创建一个新实例。参见https://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()
    }
}

关于grails - 动态抽象Grails Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568897/

相关文章:

javascript - grails gsp javascript集成,如何转义变量?

grails - DataSchema.groovy的动态模式值

java - Spring Security 的 Spring Boot UI(例如添加用户角色等)

hibernate - Grails失败时会自动重新打开连接

list - Groovy/Grails - 需要检索列表索引值

hibernate - Grails - 如何在服务中保存域对象?

grails - Grails:JIRA基本身份验证的POST方法-不支持的媒体类型

Java 从 inputStream 创建图像

grails - 如何从类名中获取类

grails - CloudFoundry之后Grails项目无法运行