简体   繁体   中英

How to use Java get function on Thymeleaf HTML?

Follow up on this question Function to determine winner

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>

Here is the funtion I'm trying to call:

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;
}

Here is the error I'm getting:

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

您不需要获取,只需尝试使用${score.winningScore}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM