首页 > 解决方案 > 不在一个 GSP 上但在另一个 Grails 上呈现的域内容列表

问题描述

对,所以我的测试视图代码是:

    @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

标签: grailsrenderinggsp

解决方案


尝试以这种方式从服务返回

@Transactional
class PostService {

    def getPosts() {
        Post.list()
    }
}

推荐阅读