Grails< g:select>困境 [英] Grails <g:select> woes

查看:118
本文介绍了Grails< g:select>困境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring Security Plugin 。默认情况下,这个插件在我的项目中创建了三个类。



org.me.example.UserRole.UserRole
org.me.example.UserRole.User
org.me.example.UserRole.Role


$ b $ < User 和 Role 类不知道但是 UserRole 类同时包含一个 User 和一个 Role 对象。 Role 对象只有一个属性,一个名为 authority 的字符串。

我的目标是在< code>中获得具有特定权威的所有用户 ; g:选择> 语句。



这是我的一个尝试:

 < g:select name =loggedBy.idfrom =$ {org.me.example.UserRole.UserRole.list(fetch:[(role.authority):ROLE_ADMIN ])}optionKey =idvalue =/> 

我知道这会返回一个 UserRole 列表,而不是用户列表。无论如何要加入我的课程,所以我可以检索我想要的数据?寻找最干净的解决方案。



更新: 我认为有一些






  class UserRole {

用户用户
角色角色

//更多东西

}






 类角色{

字符串权限

//更多内容

}





  class User {

//很多东西,但没有链接回Role类或UserRole类




$ hr






<经过一些改动后,克鲁多夫的回答对我很有帮助。 Crudof指出,包名 UserRole 不遵循Java包命名约定。这是真的,但如果我记得正确的话,Spring Security Core插件会自动创建该包和其中的类,所以我没有惹恼这个名字......但是,自从我这么做以来已经有一段时间了,所以我可以自动命名包裹。

 < g:set var =desiredRolevalue ='$ {org。 me.example.UserRole.Role.findByAuthority(ROLE_ADMIN)}'/> 
gt ;


解决方案


  1. 不符合通用的Java标准:软件包名称应该/必须以小写字符开头。否则人们会困惑,它可能是一个类(其中 UserRole 是嵌套的类 UserRole )。 li>
  2. 基本上你的代码片段很好(概念上)。请注意,您不能在双引号属性值中使用双引号!我认为这已经停止了你的进步。您可以尝试以下操作:

     < g:set var =desiredRole
    value ='$ {org .me.example.UserRole.findByAuthority(ROLE_ADMIN)}'/>

    from =$ {org.me.example.UserRole.findAllByRole(desiredRole).user}/>

    请注意,从UserRole实例访问用户实例是一种常规糖。几个例子参见 http://groovy.codehaus.org/Collections



I am using the Spring Security Plugin. By default this plugin has created three classes in my project.

org.me.example.UserRole.UserRole org.me.example.UserRole.User org.me.example.UserRole.Role

The User and Role classes are unaware of each other, but the UserRole class has both a User and a Role object. The Role object has only one attribute, a string called authority.

My objective is to obtain all Users with a certain authority in a <g:select> statement.

Here is one of my attempts:

<g:select name="loggedBy.id" from="${org.me.example.UserRole.UserRole.list(fetch:[(role.authority):"ROLE_ADMIN"])}" optionKey="id" value=""  />

I do realize that this would return a UserRole list and not a User list. Is there anyway to "join" my classes so I can retrieve the data I want? Looking for the cleanest solution.

Update:

I think there was some confusion about what my classes look like, so here are the stripped down versions.


class UserRole {

    User user
    Role role

    //more stuff

}


class Role {

    String authority

    //more stuff

}


class User {

    //lots of stuff, but nothing that links back to the Role class or the UserRole class

}


Crudof's answer worked for me after some alterations. Crudof pointed out that the package name UserRole does not follow Java package naming conventions. This is true, but if I remember right the Spring Security Core plugin automatically creates that package and the classes in it, so I didn't mess with the name... But it has been a while since I did that, so I could be mistaken about it automatically naming the package.

<g:set var="desiredRole" value='${org.me.example.UserRole.Role.findByAuthority("ROLE_ADMIN")}' />
<g:select name="loggedBy.id" from="${org.me.example.UserRole.UserRole.findAllByRole(desiredRole).user}" optionKey="id" value=""  />

解决方案

  1. Your package names are not conform to common java-standards: package names should/HAVE TO start with lower case character. Otherwise one is confused, that it might be a class (where UserRole is a nested class of UserRole).
  2. Basically your code snippet is fine (concept-wise). Note, that you cannot use double-quotes in double-quotes attribute values! I think this has stopped your progress. You can try the following:

    <g:set var="desiredRole" 
        value='${org.me.example.UserRole.findByAuthority("ROLE_ADMIN")}' />
    
    <g:select name="userSelect" 
        from="${org.me.example.UserRole.findAllByRole(desiredRole).user}" />
    

    Note that accessing the user-instances from the UserRole-instances is a groovy-sugar. See http://groovy.codehaus.org/Collections for a few examples.

这篇关于Grails&lt; g:select&gt;困境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆