【问题标题】:illegal start of simple expression and dont know why?简单表达式的非法开始,不知道为什么?
【发布时间】:2015-03-06 13:12:46
【问题描述】:
<table class="table table-hover">
        <thead>
        <th>#</th>
        <th>Poster</th>
        <th>Post</th>
        <th>Bearbeiten</th>
        </thead>
        <tbody>
        @for((result, index) <- posts.zipWithIndex) {
            @if(@result.getPoster() == @userid){
            <tr>
                <td>@{index + 1}</td>
                <td>@result.getPoster_name()</td>
                <td>@result.getPost()</td>
                <td><a href = "/post/@result.getId()">Bearbeiten</a></td>
            </tr>
            }else{
            <tr>
                <td>@{index + 1}</td>
                <td>@result.getPoster_name()</td>
                <td>@result.getPost()</td>
                <td>nicht dein Post</td>
            </tr>
            }
        }
        </tbody>
    </table>

当我尝试编译它时,我在行中得到错误非法开始简单表达式 @{index + 1} 在 if 语句中。有人可以帮我吗?我知道为什么这不起作用。

【问题讨论】:

    标签: java html scala playframework


    【解决方案1】:

    模板引擎会自动检测代码块的结尾。如果您已经用 @ 标记了您的代码,则无需在该表达式结束之前重复它。所以这将完成这项工作:

    @if(result.getPoster == userid)
    

    您在@{index + 1} 行收到错误,但问题实际上出在@if(@result.getPoster() == @userid) 行。

    我在这里也对你的代码进行了 2 处改进,如果你的 userid 是 Long,请勿将其与 == 符号进行比较,它仅适用于低于 127 的 Long 对象。

    你可以使用你的路线,而不是写href = "/post/@result.getId()"。 (动态链接)

    【讨论】:

      【解决方案2】:

      您仅在启动表达式或块时使用@。所以在这一行

      @if(@result.getPoster() == @userid)
      

      改用这个

      @if(result.getPoster == userid)
      

      【讨论】:

      • 感谢您的快速回答
      猜你喜欢
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多