gpt4 book ai didi

grails - 如何正确地将 Controller 输出呈现给GSP

转载 作者:行者123 更新时间:2023-12-02 15:00:01 25 4
gpt4 key购买 nike

在我的grails应用程序中,我有一个 Controller ,该 Controller 读取目录并返回其中的文件列表。

我的问题是,在我的GSP( View )中,它与 Controller 的输出不匹配。

这是我的 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])
}

}

这是我的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>

输出应仅列出文件名。它在 Controller 输出中显示(在控制台中显示),但是在 View 中显示如下:
[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,

Controller 的控制台输出显示以下内容:
APIWAL_525_Account_Security_otp.feature
AssignRoomToReservation.feature
DeleteAccount.feature
DeleteConsumersPaymentMethods.feature
GetActivitiesByID.feature
GetAddressType.feature
GetAddrMiscType.feature
GetAlerts.feature
GetAttractionsByID.feature

我需要怎么做才能使 View 与控制台的 Controller 输出匹配?

谢谢!

Ironmantis7x

更新!!!

为了解决我的列表问题,我这样做:
我更改了 Controller 文件以执行以下小技巧:
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

.....

谢谢你们鼓励我努力学习它,并一路为我提供帮助!

最佳答案

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

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

另一方面,您的gsp正在渲染列表,但应该给它一些样式。一个例子:
<ul>
<g:each in="${name}" var="fileName">
<li>${fileName}</li>
</g:each>
</ul>

关于grails - 如何正确地将 Controller 输出呈现给GSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34313590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com