如何正确地将控制器输出呈现给GSP

时间:2015-12-16 13:41:28

标签: grails view controller gsp

在我的grails应用程序中,我有一个控制器,它读取一个目录并返回一个文件列表。

我的问题是,在我的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.feature,

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

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

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

谢谢!

ironmantis7x

UPDATE !!!!!

解决我的上市问题我这样做了: 我改变了控制器文件来做这个小技巧:

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

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

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

并且presto !!

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

.....

感谢各位鼓励我努力学习并帮助我一路走来!

2 个答案:

答案 0 :(得分:2)

您的gsp正在呈现列表,但您的名称列表位于找到的变量中,而不是名称。无论如何,你的最后一个行动应该是:

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

另一方面,你的gsp正在渲染列表,但应该给它一些风格。一个例子:

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

答案 1 :(得分:1)

您将o / p存储在名为found的字段中,但在模型中,您使用字段name作为名称,但不包含您的o / p。您还没有声明名称,或者已经添加了任何内容,请点击它是如何在gsp上显示o / p。