Grails脚手架模板-从域类获取属性

Grails脚手架模板-从域类获取属性,grails,grails-3.0,Grails,Grails 3.0,我正在处理我的脚手架模板,更具体地说是create.gsp文件。我想获取类中定义的属性。我在网上看到了很多关于如何做到这一点的帖子,但似乎没有一篇有效 尝试了以下方法(): 我也尝试过这种方法: <% import grails.persistence.Event %> <% excludedProps = Event.allEvents.toList() << 'version' << 'dateCreated' << 'lastUp

我正在处理我的脚手架模板,更具体地说是create.gsp文件。我想获取类中定义的属性。我在网上看到了很多关于如何做到这一点的帖子,但似乎没有一篇有效

尝试了以下方法():

我也尝试过这种方法:

<% import grails.persistence.Event %>

<%  
excludedProps = Event.allEvents.toList() << 'version' << 'dateCreated' << 'lastUpdated'
persistentPropNames = domainClass.persistentProperties*.name

props = domainClass.properties.findAll { persistentPropNames.contains(it.name) && !excludedProps.contains(it.name) && (domainClass.constrainedProperties[it.name] ? domainClass.constrainedProperties[it.name].display : true) }
Collections.sort(props, comparator.constructors[0].newInstance([domainClass] as Object[]))

for (p in props) {  %>

<g:message code="${domainClass.propertyName}.${prefix}${p.name}.label" default="${p.naturalName}" />

<% } %>


我使用的是Grails3.2.4版本,脚手架插件使用fields插件。在build.gradle文件中添加插件,如下所示

dependencies {
    ...
    compile "org.grails.plugins:scaffolding"
    ...
}
使用命令安装模板

grails install-form-fields-templates
以下文件是在src/main/templates/scaffolding文件夹中创建的

  • 异步控制器.groovy
  • 异步规范groovy
  • groovy控制器
  • 创建.gsp
  • 编辑gsp
  • 普惠制指数
  • 脚手架控制器
  • show.gsp
  • 规范groovy
我已经定制了所有视图,使其看起来像推特boostrap。要呈现字段,它使用字段插件。在index.gsm视图中,我想自定义表呈现,用我的自定义代码替换g:table标记

这是为表呈现生成的部分


这是我的自定义代码,基于previos grails版本上的previos脚手架代码,并适合在grails 3.2.4版本上运行

<table class="table table-striped">
    <thead>
        <tr>
        <%
            def grailsApplication = grails.util.Holders.grailsApplication
            domainObjetc = grailsApplication.domainClasses.find { it.clazz.simpleName == className }.clazz.newInstance()
            domainClass=  grailsApplication.getDomainClass(domainObjetc.class.name)
            excludedProps = grails.persistence.Event.allEvents.toList() << 'id' << 'version'
            allowedNames = domainClass.persistentProperties*.name << 'dateCreated' << 'lastUpdated'
            props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) && it.type != null && !Collection.isAssignableFrom(it.type) }
            comparator = new org.grails.validation.DomainClassPropertyComparator(domainClass)
            Collections.sort(props, comparator)
            props.eachWithIndex { p, i ->
                if (i < 6) {
                    if (p.isAssociation()) { 
                        %><th class="header"><g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" /></th><%
                    } else { 
                        %><g:sortableColumn property="${p.name}" title="\${message(code: '${domainClass.propertyName}.${p.name}.label', default: '${p.naturalName}')}" /><%
                    }   
                }   
            }%>
            <th></th>
        </tr>
    </thead>
    <tbody>
    <g:each in="\${${propertyName}List}" var="${propertyName}">
        <tr>
        <%  props.eachWithIndex { p, i ->
                if (i < 6) {
                    if (p.type == Boolean || p.type == boolean) { %>
            <td><g:formatBoolean boolean="\${${propertyName}.${p.name}}" /></td>
        <%          } else if (p.type == Date || p.type == java.sql.Date || p.type == java.sql.Time || p.type == Calendar) { %>
            <td><g:formatDate date="\${${propertyName}.${p.name}}" /></td>
        <%          } else { %>
            <td>\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</td>
        <%  }   }   } %>
            <td class="link">
                <div class="btn-group btn-group-xs">
                    <g:link action="show" id="\${${propertyName}.id}" class="btn btn-primary btn-sm" role="button">
                        <span class="glyphicon glyphicon-eye-open"></span>
                        <g:message code="default.button.show.label" default="Show" />
                      </g:link>
                      <g:link action="edit" id="\${${propertyName}.id}" class="btn btn-primary btn-sm" role="button">
                        <span class="glyphicon glyphicon-pencil"></span>
                        <g:message code="default.button.edit.label" default="Edit" />
                      </g:link>
                </div>
            </td>
        </tr>
    </g:each>   
    </tbody>
</table>


我使用的是Grails3.2.4版本,脚手架插件使用fields插件。在build.gradle文件中添加插件,如下所示

dependencies {
    ...
    compile "org.grails.plugins:scaffolding"
    ...
}
使用命令安装模板

grails install-form-fields-templates
以下文件是在src/main/templates/scaffolding文件夹中创建的

  • 异步控制器.groovy
  • 异步规范groovy
  • groovy控制器
  • 创建.gsp
  • 编辑gsp
  • 普惠制指数
  • 脚手架控制器
  • show.gsp
  • 规范groovy
我已经定制了所有视图,使其看起来像推特boostrap。要呈现字段,它使用字段插件。在index.gsm视图中,我想自定义表呈现,用我的自定义代码替换g:table标记

这是为表呈现生成的部分


这是我的自定义代码,基于previos grails版本上的previos脚手架代码,并适合在grails 3.2.4版本上运行

<table class="table table-striped">
    <thead>
        <tr>
        <%
            def grailsApplication = grails.util.Holders.grailsApplication
            domainObjetc = grailsApplication.domainClasses.find { it.clazz.simpleName == className }.clazz.newInstance()
            domainClass=  grailsApplication.getDomainClass(domainObjetc.class.name)
            excludedProps = grails.persistence.Event.allEvents.toList() << 'id' << 'version'
            allowedNames = domainClass.persistentProperties*.name << 'dateCreated' << 'lastUpdated'
            props = domainClass.properties.findAll { allowedNames.contains(it.name) && !excludedProps.contains(it.name) && it.type != null && !Collection.isAssignableFrom(it.type) }
            comparator = new org.grails.validation.DomainClassPropertyComparator(domainClass)
            Collections.sort(props, comparator)
            props.eachWithIndex { p, i ->
                if (i < 6) {
                    if (p.isAssociation()) { 
                        %><th class="header"><g:message code="${domainClass.propertyName}.${p.name}.label" default="${p.naturalName}" /></th><%
                    } else { 
                        %><g:sortableColumn property="${p.name}" title="\${message(code: '${domainClass.propertyName}.${p.name}.label', default: '${p.naturalName}')}" /><%
                    }   
                }   
            }%>
            <th></th>
        </tr>
    </thead>
    <tbody>
    <g:each in="\${${propertyName}List}" var="${propertyName}">
        <tr>
        <%  props.eachWithIndex { p, i ->
                if (i < 6) {
                    if (p.type == Boolean || p.type == boolean) { %>
            <td><g:formatBoolean boolean="\${${propertyName}.${p.name}}" /></td>
        <%          } else if (p.type == Date || p.type == java.sql.Date || p.type == java.sql.Time || p.type == Calendar) { %>
            <td><g:formatDate date="\${${propertyName}.${p.name}}" /></td>
        <%          } else { %>
            <td>\${fieldValue(bean: ${propertyName}, field: "${p.name}")}</td>
        <%  }   }   } %>
            <td class="link">
                <div class="btn-group btn-group-xs">
                    <g:link action="show" id="\${${propertyName}.id}" class="btn btn-primary btn-sm" role="button">
                        <span class="glyphicon glyphicon-eye-open"></span>
                        <g:message code="default.button.show.label" default="Show" />
                      </g:link>
                      <g:link action="edit" id="\${${propertyName}.id}" class="btn btn-primary btn-sm" role="button">
                        <span class="glyphicon glyphicon-pencil"></span>
                        <g:message code="default.button.edit.label" default="Edit" />
                      </g:link>
                </div>
            </td>
        </tr>
    </g:each>   
    </tbody>
</table>


在Grails 4中,在我的示例Grails 4.0.10中,要获取域列表,必须将过程更改为:

MappingContext mappingContext = grailsApplication.getMappingContext()
def entities = mappingContext.getPersistentEntities()
并获得一个实体,具体如下:

def entity = mappingContext.getPersistentEntity(fullEntityName) 
要获取实体的持久属性,请执行以下操作:

def persistentProperties = entity.getPersistentProperties()
要获得排除的PRPoerty,请执行以下操作:

def excludedProps = Event.allEvents.toList() << 'id' << 'version'

因此,我们可以遵循前面文章中建议的相同逻辑

在Grails 4中,在我的例子Grails 4.0.10中,要获取域列表,必须将过程更改为:

MappingContext mappingContext = grailsApplication.getMappingContext()
def entities = mappingContext.getPersistentEntities()
并获得一个实体,具体如下:

def entity = mappingContext.getPersistentEntity(fullEntityName) 
要获取实体的持久属性,请执行以下操作:

def persistentProperties = entity.getPersistentProperties()
要获得排除的PRPoerty,请执行以下操作:

def excludedProps = Event.allEvents.toList() << 'id' << 'version'

因此,我们可以遵循前面文章中建议的相同逻辑

可能重复的“为您的查询查看此答案”:可能重复的“为您的查询查看此答案”:在Grails 3.3.10上尝试,但未获得此类属性:Grails for class:groovy.lang.Binding,在访问Grails.*包时发生。此外,DomainClassPropertyComparator在3.3.10中被弃用。检查是否正确,DomainClassPropertyComparator在3.3.10中已被弃用。在Grails 3.3.10上尝试,但未获得此类属性:Grails for class:groovy.lang.Binding,在访问Grails.*包时发生。此外,DomainClassPropertyComparator在3.3.10中被弃用。检查是否正确,DomainClassPropertyComparator在3.3.10中不推荐使用。