简体   繁体   English

Grails从一个域读取到另一个域

[英]Grails read from one domain to another domain

I created 2 domain class which is 我创建了2个域类

Duty.groovy: Duty.groovy:

    class Duty {
               String username
               String duty
    }

User.groovy: User.groovy:

    class User {
               String username
    }

I already assigned some users in Bootstrap.groovy, I want to create a duty list function to assigned duty to users. 我已经在Bootstrap.groovy中分配了一些用户,我想创建一个职责列表函数来为用户分配职责。 So in my duty list, it should include a drop-down list which contains list of users from User domain. 因此,在我的职责列表中,它应该包含一个下拉列表,其中包含来自“用户”域的用户列表。

But when I import testApp.User at my gsp (in Duty domain) file and created a taglib to display the userlist, my compiler shows that No such property. 但是,当我在gsp(在Duty域中)文件中导入testApp.User并创建一个taglib来显示用户列表时,我的编译器显示没有这样的属性。 I would like to ask for suggestions for best way to display the list of users from my Duty domain to User domain? 我想问一些建议,以最佳方式显示从我的责任域到用户域的用户列表?

Here is other code: SelectUserTagLib.groovy 这是其他代码:SelectUserTagLib.groovy

    import HR_System.User
    def userInstance = new User()
    def displayType = { attrs,body -> 
    def name = attrs.name 

    out << "<select id=\"${name}\" name=\"${name}\" require=\"\" >"
    out << "<g:each in='${userInstanceList}'' status='i' var='userInstance'>"
    out << "<option value=\"${name}\">${fieldValue(bean: userInstance, field: "username")}</option>"
    out << "</g:each>"
    out << "</select>"
}

_form.gsp: _form.gsp:

    <name:displayType name="username" />

I think you don't really need custom taglib for such purpose. 我认为您实际上不需要为此目的使用自定义taglib。 You can do it with simple g:select. 您可以使用简单的g:select来实现。

<g:select name="user"
          from="${User.list()}"/>

You can read about it in grails docs: https://docs.grails.org/latest/ref/Tags/select.html . 您可以在grails文档中阅读它: https : //docs.grails.org/latest/ref/Tags/select.html

Please ask if you have any questions. 请询问您是否有任何疑问。

If you adjust your model to be really a domain model so it looks like: 如果您将模型调整为真正的域模型,则它看起来像:

    class User {
               String username
    }

    class Duty {
               User user
               String duty
    }

then Grails would scaffold the GSP-view for your Duty class automagically with the User -dropdown included 那么Grails会自动使用您的“ User下拉列表为您的Duty类提供GSP视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM