java - 如何在java play框架中检查数据库表是否为空

标签 java playframework playframework-2.0

我能够从数据库中检索数据,但无法添加在数据库表为空时显示警报的条件。

这是我的代码:

index.scala.html:

@(formList: List[Users],form: Form[Users])

@main("history") {

    @for(i <- Users.all()) {

        @if(i.client.equalsIgnoreCase("potato")) {

            <Table>
                <tbody>
                    <tr>
                        <td><a href="/#"><i >@i.firstname @i.lastname </i></a></td>
                        <td><a href="/#"><i>@i.phonenumber</i></a></td>
                        <td><a href="/#"><i>@i.amount</i></a></td>
                        <td><a href="/#"><i>@i.doneAt</i></a></td>
                        <td><a href="@routes.Application.edit(i.id)"><i><span class="glyphicon glyphicon-pencil "></span></i></a></td>
                        <td><a href="@routes.Application.delete(i.id) "><i><span class="glyphicon glyphicon-trash "></span></i></a></td>
                    </tr>
                </tbody>
            </table>
        }
    }
}

Controller 方法:

public static Result history(long id) {
    Form<Users> taskData = form(Users.class);
    return ok(history.render(Users.all(), taskData));
}

最佳答案

tl;dr 将 User.all() 移动到 Controller 并通过模板参数传递它,然后添加 if 语句以在列表为空时呈现警报。

<小时/>

您必须添加 if 语句来检查用户列表是否为空,如果为空则显示警报。

@if (Users.all().isEmpty) {
  // Show the alert.
}

为了避免两次获取用户,您可以使用defining函数。

@defining(Users.all()) { users =>
  @if (users.isEmpty) {
    // Show the alert.
  }
  @for(user <- users) {
    …
  }
}

但我强烈建议将用户获取移至 Controller ,并使模板接受用户列表作为参数。这样您就可以保持模板简单。当它被渲染时,它只会呈现数据,而不是潜在地执行繁重的工作,例如从数据库中获取记录。

最终结果可能如下所示。

模板

@(users: List[Users], formList: List[Users],form: Form[Users])

@main("history") {

    @if (users.isEmpty) {
        <div class="alert">
            …
        </div>
    }

    @for(i <- users) {

        @if(i.client.equalsIgnoreCase("potato")) {

            <Table>
                <tbody>
                    <tr>
                        <td><a href="/#"><i >@i.firstname @i.lastname </i></a></td>
                        <td><a href="/#"><i>@i.phonenumber</i></a></td>
                        <td><a href="/#"><i>@i.amount</i></a></td>
                        <td><a href="/#"><i>@i.doneAt</i></a></td>
                        <td><a href="@routes.Application.edit(i.id)"><i><span class="glyphicon glyphicon-pencil "></span></i></a></td>
                        <td><a href="@routes.Application.delete(i.id) "><i><span class="glyphicon glyphicon-trash "></span></i></a></td>
                    </tr>
                </tbody>
            </table>
        }
    }
}

Controller

public static Result history(long id) {
    Form<Users> taskData = form(Users.class);
    return ok(history.render(Users.all(), Users.MTN(), taskData));
}

关于java - 如何在java play框架中检查数据库表是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38794643/

相关文章:

java - 从 EnumSet 中检索数字位值

javascript - 是否可以在 Play Framework View 中 console.log ?

java - Playframework 建议导入语句不起作用?

javascript - 无法通过ajax将字符串传递给 Controller

scala - post 方法渲染 403 forbidden 页面而不是执行 post 方法代码

mysql - 将 Scala 和 Play-Slick 连接到 AWS MySQL

java - 如何使用正则表达式解析属性

Java 8方法签名同一方法的不同版本

java - 为什么我的方法在 System.in.read() 循环后被调用 3 次

hibernate - 如何在 JPA/Play 框架的 LIKE 子句中使用百分比?