Search code examples
grailsviewcontrollergsp

how to correctly render controller output to GSP


in my grails application, I have a controller that reads a directory and returns a list of files in it.

My issue is that in my GSP (view) it is not matching the out put from the controller.

Here is my controller:

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])      
    }

}

here is my GSP (view):

<%@ 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>

The output should just list the file names. It does in the controller output (shown in console), but in the view it shows this:

[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,

the console output for the controller shows this:

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

What do i need to do to make the view match the controller output from console??

thanks!

ironmantis7x

UPDATE!!!!!

to solve my listing issue I did this: I changed the controller file to do this little trick:

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

then to make the gsp list the file names on a new line, I did this:

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

And 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

.....

Thanks guys for encouraging me to struggle to learn it and helping me along the way!


Solution

  • Your gsp is rendering the list but your list of names is in the variable found, not name. Anyway, your last action line should be:

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

    In other hand, your gsp is rendering the list, but should give it some style. An example:

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