0

我正在开发一个 Grails 应用程序,其中一些子域类扩展了一个基本抽象域类。我还有一个包含许多抽象域类的父域类。当您为父级运行“grails generate-all”然后创建一个带有一堆子级的父级时,列表视图中的链接指向没有控制器或视图的抽象域类。让这种观点发挥作用的最简单方法是什么?如果问题有点模糊,下面是一些伪代码,以帮助使事情更清楚。

领域类

abstract class Automobile{
    String description
}

class Car extends Automobile{}

class Truck extends Automobile{}

class Dealership{
   static hasMany = [automobiles:Automobile]
}

用汽车和卡车创建经销商

def car = new Car(description:"Toyota Camry").save()
def truck = new Truck(description : "Toyota Tacoma").save()

def dealership = new Dealership()
dealership.addToAutomobiles(car)
dealership.addToAutomobiles(truck)
dealership.save()

为 Dealership 生成的 show.gsp 将如下所示:

<htm>
 <table>
  <tr>
    <th>Id</th>
    <td>${dealershipInstance.id}</td>
  </tr>
  <tr>
    <th>Automobiles</th>
    <td>
      <ul>
        <g:each in="${dealershipInstance.automobiles}" status="i" var="automobileInstance">
          <li><g:link action="show" id="${automobileInstance.id}">${automobileInstance.description}</g:link></li>
        </g:each>
      </ul>
    </td>
  </tr>
 </table>
</html>

问题在于与汽车实例的链接,因为基础抽象类汽车没有自己的视图。因此“显示”操作返回 404。在这种情况下,链接到适当的域类的最佳方式是什么?可能有一个简单的方法来解决这个问题,我只是还没有找到它。

在此先感谢您的帮助。

4

2 回答 2

1

谢谢克罗托。您的问题和回答使我找到了与您类似的解决方案。不过,您不必将类名属性添加到您的 Domain 类。它已经在那里了。

使用 class.simpleName 来获取您需要的内容。

例如:

<g:link controller="${p.class.simpleName}" action="show" id="${p.id}">${p.description}</g:link>
于 2013-12-09T01:18:37.150 回答
1

为了避免在我的 gsp 中编写 groovy 或 java 代码,我添加了一个带有类名的字符串变量,以便在 gsp 中轻松引用。

<g:link controller="${p.classname}" action="show" id="${p.id}">${p.description} ${p?.classname}</g:link>
于 2012-06-07T13:09:57.557 回答