Grails 控制器动态查找器

发布于 2024-11-08 17:05:35 字数 1096 浏览 0 评论 0原文

我对 Grails 有点陌生,所以我需要有关此事的帮助。想象一下我有这些类:

Class User{
String name
String nr

}

class Computer{

String processor
String ram
User user
}

Class Room{
String roomNr
Computer computer
}

我想做一个这样的控制器:

def print= {

        def user = User.get(1)

        def computer = Computer.findAllByUser(user) // all computers filtered by User

        [computer: computer]       
    }

控制器运行良好,但我希望不仅能够传递计算机实例,而且还能够传递具有该计算机 ID 的房间。我不知道该怎么做,但会是这样的(相同的控制器,相同的操作):

def print= {

        def user = User.get(1)

        def computer = Computer.findAllByUser(user) // all computers filtered by User
        def room = Room.findAllByComputer(computer) ##

        [computer: computer]       
    }

嗯,这是错误的,因为 ### 所在的位置,“计算机”代表 id 列表,而不是一个单一的ID。当我传递要在 ag 内的 gsp 中打印的计算机列表时:每个标签如下所示:(it.Processor、it.Ram)。我不知道如何获取每个标签中使用当前计算机的所有房间。这有点难以解释,但基本上我的输出是:

<g:each in="${computer}"
Computer1:
Processor: ${it.processor}
Ram: ${it.ram}
Room: ????? (How to get here??)
</g:each>

I'm kinda new to Grails so I need help with this matter. Imagine I have these classes:

Class User{
String name
String nr

}

class Computer{

String processor
String ram
User user
}

Class Room{
String roomNr
Computer computer
}

I want to do a controller like this:

def print= {

        def user = User.get(1)

        def computer = Computer.findAllByUser(user) // all computers filtered by User

        [computer: computer]       
    }

The controller works good, but I want to be able to pass not only the computer instances, but also the Room's with that computer id's. I dont know how to do it, but would be something like this (same controller, same action):

def print= {

        def user = User.get(1)

        def computer = Computer.findAllByUser(user) // all computers filtered by User
        def room = Room.findAllByComputer(computer) ##

        [computer: computer]       
    }

Well, this is wrong because, where the ### is, 'computer' represents a list of id's and not a single id. As I am passing a list of computers to be printed in my gsp inside a g: each tag like this: (it. Processor, it. Ram). I dont know how to get all the rooms which are using the current computer in the each tag. This is a bit hard to explain, but basically my output would be:

<g:each in="${computer}"
Computer1:
Processor: ${it.processor}
Ram: ${it.ram}
Room: ????? (How to get here??)
</g:each>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自在安然 2024-11-15 17:05:35

使关系成为双向的。现在,您存储哪个房间有哪台计算机,但您不会走相反的路,因此您可以跟随计算机到其房间。查看

http://grails .org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html

特别是第 5.2.1.2 节,双向一对多关系。

请注意,Grails 是建立在最佳技术之上的。其中之一是休眠。熟悉底层技术只会对你有帮助。

所以毕竟,我得到的是你的 Computer 类应该有一个对其所在房间的引用。 Room 类应该有一个计算机列表(假设一个房间中可以有不止一台计算机)。您将遇到的困难是理解 hibernate 的级联行为,这取决于您如何建立关系。如果您遇到问题,请更新此问题或针对特定问题开始新问题。

make the relationship bidirectional. Right now you store which Room has which Computer, but you don't go the other way, so you can follow a Computer to its Room. Check out

http://grails.org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html

specifically section 5.2.1.2, on bidirectional one-to-many relationships.

Be aware that Grails is build on best of breed technologies. One of those is hibernate. Being familiar with the underlying technologies will only help you.

So after all that, what I am getting at is your Computer class should have a reference to the Room that it is in. The Room class should have a list of Computers (assuming more than one computer can be in a room). The difficulty you will have is in understanding the cascading behaviour of hibernate, which depends on how you set up the relationship. If you run into trouble update this question or start a new one with the specific issue.

初心未许 2024-11-15 17:05:35

我想出了类似的东西,但我没有测试它。

控制器:

def print = {

  def user = User.get(1)
  def computer = Computer.findAllByUser(user)
  def rooms = [:]

  // get rooms for that computer
  computer.each{

    def computerRooms = Room.findAllByComputer(it) // find all rooms where current computer is
    rooms.put(it.id, computerRooms) // map all the rooms with computer id

  }

  [computer: computer, rooms: rooms]      
}

视图:

<g:each in="${computer}">
  Computer1:
  Processor: ${it.processor}
  Ram: ${it.ram}
  Rooms: <g:join in="${rooms[it.id]}" delimiter=", "/>
</g:each>

房间地图使用计算机 ID 作为键,并使用该计算机所在的所有房间的列表作为值。当然,如果我理解正确的话;)

编辑

如果您需要访问房间详细信息,您可以将 join 标记替换为另一个 each 标记,如下所示:

<g:each in="${computer}" >
  Computer1:
  Processor: ${it.processor}
  Ram: ${it.ram}
  Rooms: <br/>
  <g:each in="${rooms[it.id]}">
    Room id: ${it.id}
    Room number: ${it.number}
  </g:each>
</g:each>

您还可以实现RoomtoString() 方法,因为 join 标签使用它来渲染文本:

Class Room{
  String roomNr
  ...

  String toString(){
    roomNr
  }
}

现在当您使用 join 标签时在 Room 类实例列表中,它将打印 room 数字。

I came up with something like this, but I didn't test it.

Controller:

def print = {

  def user = User.get(1)
  def computer = Computer.findAllByUser(user)
  def rooms = [:]

  // get rooms for that computer
  computer.each{

    def computerRooms = Room.findAllByComputer(it) // find all rooms where current computer is
    rooms.put(it.id, computerRooms) // map all the rooms with computer id

  }

  [computer: computer, rooms: rooms]      
}

View:

<g:each in="${computer}">
  Computer1:
  Processor: ${it.processor}
  Ram: ${it.ram}
  Rooms: <g:join in="${rooms[it.id]}" delimiter=", "/>
</g:each>

Rooms map uses computer id as a key and list of all rooms where that computer is as value. Of course If I understood you correctly ;)

EDIT

If you need access room details, you can replace join tag with another each tag, like this:

<g:each in="${computer}" >
  Computer1:
  Processor: ${it.processor}
  Ram: ${it.ram}
  Rooms: <br/>
  <g:each in="${rooms[it.id]}">
    Room id: ${it.id}
    Room number: ${it.number}
  </g:each>
</g:each>

And you can also implement the Room class toString() method, because join tag uses it to render text:

Class Room{
  String roomNr
  ...

  String toString(){
    roomNr
  }
}

Now when you use join tag on list of Room class instances, it will print room number.

初见终念 2024-11-15 17:05:35

看来您没有将 room 变量发送到视图

[computer: computer]

应该是:

[computer:computer, room:room]

It looks like you don't send the room variable to the view

[computer: computer]

Should be:

[computer:computer, room:room]
走野 2024-11-15 17:05:35

您可以使用;获取内的房间参考。这样,控制器就不会关心房间。一般来说,我喜欢收集为控制器提供 GSP 的所有相关信息,但也有例外,例如某些辅助细节。

You can use <g:set> to acquire a room reference inside your <g:each>. That way, the controller is not concerned with rooms. Generally, I like to gather all pertinent information that feeds the GSP in the controller, but there are exceptions, as in the case of some ancillary details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文