如何正确地将控制器输出渲染到 GSP

how to correctly render controller output to GSP

在我的 grails 应用程序中,我有一个控制器读取目录和 returns 其中的文件列表。

我的问题是在我的 GSP(视图)中它与控制器的输出不匹配。

这是我的控制器:

package frametest

import groovy.io.FileType

class Read_dirController {

    def index() { 

        def list = []

        def dir = new File("D:\TestRepoLocal\APIAutomation\src\test\cucumber\features")
        dir.eachFileRecurse (FileType.FILES) { file ->
          list << file
        }

        list.each {
            println it.name
          }

        def found = []
        dir.eachFileMatch(FileType.ANY, ~/.feature/) {
            found << it.name
        }

        render(view: "index",  model: [name:name])      
    }

}

这是我的 GSP(视图):

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>Welcome to The Marriot test Page</title>
</head>
<body>
  <div class="body">
    <h2>Marriott Automation Test Page</h2>
    <br>
    <p></p>
    <h3>This is where we can launch our tests and get reports</h3>
    <br>
    <br>
    <p></p>

    ${name}

  </div>
</body>
</html>

输出应该只列出文件名。它在控制器输出中执行(显示在控制台中),但在视图中显示如下:

[D:\TestRepoLocal\APIAutomation\src\test\cucumber\features\APIWAL_525_Account_Security_otp.feature, D:\TestRepoLocal\APIAutomation\src\test\cucumber\features\AssignRoomToReservation.feature, D:\TestRepoLocal\APIAutomation\src\test\cucumber\features\DeleteAccount.feature, D:\TestRepoLocal\APIAutomation\src\test\cucumber\features\DeleteConsumersPaymentMethods.feature, D:\TestRepoLocal\APIAutomation\src\test\cucumber\features\GetActivitiesByID.feature, D:\TestRepoLocal\APIAutomation\src\test\cucumber\features\GetAddressType.feature, D:\TestRepoLocal\APIAutomation\src\test\cucumber\features\GetAddrMiscType.feature, D:\TestRepo

Local\APIAutomation\src\test\cucumber\features\GetAlerts.特征,

控制器的控制台输出显示如下:

APIWAL_525_Account_Security_otp.feature
AssignRoomToReservation.feature
DeleteAccount.feature
DeleteConsumersPaymentMethods.feature
GetActivitiesByID.feature
GetAddressType.feature
GetAddrMiscType.feature
GetAlerts.feature
GetAttractionsByID.feature

我需要做什么才能使视图与控制台的控制器输出匹配?

谢谢!

ironmantis7x

更新!!!!!

为了解决我的列表问题,我这样做了: 我改变了控制器文件来做这个小技巧:

render(view: "index",  model: [name:list.name])

然后为了让 gsp 在新行中列出文件名,我这样做了:

<ul>
        <g:each in="${name}" var="fileName">
               ${fileName}<br>
        </g:each>
    </ul>

转瞬即逝!!

This is where we can launch our tests and get reports



APIWAL_525_Account_Security_otp.feature




 AssignRoomToReservation.feature
 DeleteAccount.feature
 DeleteConsumersPaymentMethods.feature
 GetActivitiesByID.feature
 GetAddressType.feature
 GetAddrMiscType.feature

.......

感谢你们鼓励我努力学习并一路帮助我!

您将 o/p 存储在名为 found 的字段中,但在模型中您使用字段 name 作为名称包含在内,其中不包含您的 [=14] =].你还没有声明名称,或者没有向它添加任何东西,我知道它是如何显示 o/p 即使在 gsp 上也是如此。

您的 gsp 正在呈现列表,但您的姓名列表位于找到的变量中,而不是名称中。不管怎样,你的最后一行应该是:

render(view: "index",  model: [name: found]) 

另一方面,您的 gsp 正在呈现列表,但应该给它一些样式。一个例子:

<ul>
    <g:each in="${name}" var="fileName">
           <li>${fileName}</li>
    </g:each>
</ul>