【发布时间】:2021-05-03 03:00:41
【问题描述】:
是的,所以我的测试视图代码是:
@Secured(["IS_AUTHENTICATED_REMEMBERED", "IS_AUTHENTICATED_FULLY"])
def list(){
def allPosts = postService.getPosts()
}
<body>
<h1>Welcome to the main feed, <sec:loggedInUserInfo field="username" var="username"/></h2>
<g:if test="${flash.message}">
<div class="messageContainer">
<div style="display: block">${flash.message}</div>
</div>
</g:if>
<p/>
<div>
<g:each var="post" in="${allPosts}">
<p>Last Name: ${post.content}</p>
<p>First Name: ${post.content}</p>
</g:each>
</div>
</body>
</html>
这行得通,
这只是为了测试是否还有其他问题,因为我希望它可以解决这个问题:
@Secured(["IS_AUTHENTICATED_REMEMBERED", "IS_AUTHENTICATED_FULLY"])
def wall() {
def allPosts = postService.getPosts()
profile = springSecurityService.currentUser.getProfile()
if(!profile){
redirect(controller: "profile", action: "viewProfile")
}else {
[profile: profile]
}
}
和
<!doctype html>
<html>
<head>
<meta name="layout" content="main"/>
</head>
<body>
<h1>Welcome to the main feed, <sec:loggedInUserInfo field="username" var="username"/></h2>
<g:if test="${flash.message}">
<div class="messageContainer">
<div style="display: block">${flash.message}</div>
</div>
</g:if>
<p/>
<div>
<h3>
What are you thinking?
</h3>
</p>
<div>
<g:form action="addPost">
<g:textArea class="postContent" id="postContent" name="content" rows="3" cols="50"/><br/>
<g:submitButton class="postSubmit loginButton" name="post" value="Post"/>
</g:form>
<div>
</div><br/>
<div class="PostContainer">
<g:each var="post" in="${allPosts}">
<p>${post.content}</p>
<p>${post.content}</p>
</g:each>
</div>
</div>
</body>
</html>
但帖子和内容不会在第二个示例中呈现
还有 postService:
@Transactional
class PostService {
def getPosts() {
[allPosts:Post.list()]
}
}
任何对此的见解将不胜感激。
我在浏览器上使用的是 windows 10 和 chrome
【问题讨论】:
-
我很惊讶第一个作品。您是说如果您有一个名为
list的控制器操作,并且该操作中有 1 行代码def allPosts = postService.getPosts(),那么引用<g:each var="post" in="${allPosts}">的 GSP 有效吗?我希望${postList}可以工作,但不是${allPosts}。只要确保我明白你在说什么。 -
@JeffScottBrown allPosts 只是列表的参考,它指向服务 getPosts() 方法。事实证明,我需要做的就是将 allPosts 放入无法正常工作的控制器中,放入 if 语句的 else 部分,我在其中调用配置文件来呈现页面,它使 allPosts 为空。
-
“它使 allPosts 为空” - 完全正确。您展示的代码将创建一个名为
postList的模型变量,而不是allPosts。