如何在 Thymeleaf HTML 上使用 Java get 函数? [英] How to use Java get function on Thymeleaf HTML?

查看:44
本文介绍了如何在 Thymeleaf HTML 上使用 Java get 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跟进这个问题确定获胜者的函数

如何使用 thymeleaf HTML 表单中链接问题中所述的 java 函数?

How do I use the java function stated on the linked question in thymeleaf HTML form?

这是我到目前为止的代码,但函数调用不起作用:

Here is the code I have so far, but the function call doesn't work:

    <tr th:each="score : ${score}">
        <td th:text="${score.player1name}"></td>
        <td th:text="${score.player2name}"></td>
        <td>
            <ul th:each="round : ${score.roundScores}">
                <li th:text="${round}"/>
            </ul>
        </td>
        <td th:text="${score.finalscore}"></td>
<td th:text="score.winner : ${score.getWinningScore()}">
        <td th:text="${score.sportstype}"></td>
        <td><a class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this score?')" th:href="@{/delete/{id}(id=${score.gameid})}">Delete</a></td>
        <td><a class="btn btn-primary"  th:href="@{/edit/{id}(id=${score.gameid})}">Edit</a></td>
    </tr>
</table>

 <a  href="/welcome" class="btn btn-success">New Game</a>

这是我要调用的函数:

public String getWinningScore() {
    //Lambda Expressions to convert String array to Int array and calculate sums
    int sumOfscore1 = Arrays.stream(player1score).mapToInt(Integer::parseInt).sum();
    int sumOfscore2 = Arrays.stream(player2score).mapToInt(Integer::parseInt).sum();
    if (sumOfscore1 > sumOfscore2) {
        winner = player1name;
    } else if (sumOfscore2 > sumOfscore1) {
        winner = player2name;
    }
    return winner;
}

这是我得到的错误:

引起:org.attoparser.ParseException:评估 SpringEL 表达式的异常:score.getWinningScore()"(模板:scorelist" - 第 31 行,第 8 列)

Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "score.getWinningScore()" (template: "scorelist" - line 31, col 8)

推荐答案

显然我的 html 前面有空字段,当播放的轮数少于最大数量时,它用空白字符串填充其余部分.

Apparently I had empty fields on my html front, and when less than max amount of rounds where played it filled the rest with blank string.

空字符串不能输入到字符串列表中.

Blank string cannot be inputted to a string list.

这篇关于如何在 Thymeleaf HTML 上使用 Java get 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆