【问题标题】:Grails GSP Formatting IssuesGrails GSP 格式问题
【发布时间】:2017-10-05 08:21:56
【问题描述】:

我正在试用 Grails,但在使用 GSP 时遇到了一些问题。

我有三个班级,分别是:作者、书籍和奖项。一位作者可以拥有多本书,每本书可以有多个奖项。以下是域控制器:

class Author {

    String  authorName
    static  hasMany = [books : Book]

}

class Book {

    String      bookName
    BigDecimal  price

    static      belongsTo=[author : Author]
    static      hasMany=[awards: Award]
}

class Award {

    String  awardName
    Date    awardDate

    static  belongsTo = [book : Book]

}

以下是我的普惠制:

        <table>
            <th >Author Name</th>
            <th >Books</th>
            <th >Awards</th>

            <g:each in="${authorList}" var="author" status="i">
                <tr>

                    <td> ${author.authorName} </td>
                    <td> ${author.books} </td>
                    <td> ${author.books.awards} </td>
                </tr>
            </g:each>
        </table>

目前正在显示:

书籍为:[第 2 册,第 1 册]
奖项为:[[], [Award 1]]

我想将它们显示为: 图书: 1. 书 2 2. 第 1 册

奖项:
1.
2.奖励1

我可以将 Book 和 Award 都放入一个对象中并用于迭代它们吗?

提前谢谢你。

【问题讨论】:

    标签: grails gsp


    【解决方案1】:

    ${author.books}${author.books.awards} 是一个 SET,所以你可以

    <g:each in="${author.books}" var="book">
        <tr>
            <td> ${book?.bookName} </td>
            <td> ${book?.price} </td>
        </tr>
    </g:each>
    

    【讨论】:

    • 非常感谢!这正是我需要的!干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多