【问题标题】:G:each is not showing anythingG:每个都没有显示任何东西
【发布时间】:2016-08-11 14:04:39
【问题描述】:

控制器类

class EntidadController {
    def index(){
        def entidades = Entidad.list()
        [entidades:entidades]
        render(view:"index")
        for (x in entidades)
        {
            print(x.nombreEntidad)
        }
    }
}

领域类

    class Entidad {

        String nombreEntidad
        int porcentaje
        static hasOne = [kiosko: Kiosko]
        static belongsTo = [adminCreador: Administrador,entidadSuperior: Entidad]
        static hasMany = [adminEntidad: Administrador, entidadesInferiores: Entidad]
        static constraints = {
            kiosko nullable:true        
            nombreEntidad nullable : false
            adminCreador nullable : true 
            adminEntidad nullable : true 
            entidadSuperior nullable : true 
            entidadesInferiores nullable : true
        }



}

在 gsp 中查看

<!DOCTYPE html>
<html>
<head>
<title>test</title>


</head>

<body>
<p>test</p>
<div controller="entidadController">
<g:each in="${entidades}"  var="x">
                <tr>
                        <td>${x.id}</td>

                </tr>
                </g:each>
</div>
</body>

</html>

我认为代码很好,但我不知道为什么在 g:each 标记处没有显示任何内容,我尝试在 gsp 页面中使用静态数组并且 g 每个都有效,我在控制台打印并且数组“entidades”有对象在它上面,就像我的视图无法从控制器读取数据

【问题讨论】:

    标签: grails model-view-controller grails-orm


    【解决方案1】:

    可能是这样的渲染语句:

    render(view:'index', model:[entidades: entidades])
    

    【讨论】:

      【解决方案2】:

      如果您不需要在视图中进行分页和排序,您可以在 gsp 上这样做

      ...
      <body>
      <p>test</p>
      <div controller="entidadController">
      <g:each in="${Entidad.list()}"  var="x">
                  <tr>
                          <td>${x.id}</td>
      
                  </tr>
                  </g:each>
      </div>
      </body>
      ...
      

      【讨论】:

        【解决方案3】:

        问题出在这里:

        def index(){
            ...
            [entidades:entidades]
            render(view:"index")
            ...
        }
        

        您没有将entidades 发送到视图。这可以通过这种方式纠正 (render docs):

        def index(){
            ...
            render(view:"index", model:[entidades:entidades])
            ...
        }
        

        另外,如果你的视图和你的动作同名,你不需要显式地使用 render 方法,而是使用 map 作为动作的最后一行(这是 grails 约定)。像这样:

        def index(){
            ...
            [entidades:entidades]
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-29
          相关资源
          最近更新 更多