简体   繁体   中英

Thymeleaf and Spring Boot causing TypeMismatchException

When working with thymeleaf I suddenly got the following error when clicking one of the images:

Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "style.css"]

I tried to use standard values to remove this standard mistake but it did not help. Does anybody have a tip where this error could come from?

INFO: This error is causing my files "styles/style.css" "styles/full.jpg" "styles/empty.jpg" to not load.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
    <meta charset="UTF-8">
    <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <link rel="stylesheet" th:href="@{/styles/style.css}"/>
            <title th:text="${title}"></title>
        </head>
        <style>
            input {
                margin: 8px;
                padding: 6px;
            }

            input:focus {
                filter: drop-shadow(2px 2px 2px rgb(60, 60, 60));
                transition-duration: 0.3s;
            }

            #rating {

            }
        </style>
        <body style="text-align: center; margin: 0px; padding: 0px;">
        <div style="filter: drop-shadow(1px 1px 1px rgb(60,60,60));">
            <h2><span style="color: lightgreen;">lightrate</span> : the next level rating tool</h2>
            <hr>
        </div>
        <div id="rating" style="width: 600px; height: 400px; margin: auto; border: 1px solid lightgray; border-radius: 5px; margin-top: 60px; -webkit-box-shadow: 1px 0px 20px -5px rgba(0,0,0,0.75);
-moz-box-shadow: 1px 0px 20px -5px rgba(0,0,0,0.75);
box-shadow: 1px 0px 20px -5px rgba(0,0,0,0.75);">
            <h1 th:text="${rating.getTitle().getContent()}"></h1>
            <div th:text="'created on ' + ${rating.getDateOfCreation().toLocalDate()}"></div>
            <hr>
            <h4 th:text="${rating.getDescription().getContent()}"></h4>
            <!--<form action="#" method="post" th:action="'/' + ${rating.getRatingID().getId().toString()}">-->
            <img class="img1 star" onclick="rate(1)" src="styles/empty.jpg">
            <img class="img2 star" onclick="rate(2)" src="styles/empty.jpg">
            <img class="img3 star" onclick="rate(3)" src="styles/empty.jpg">
            <img class="img4 star" onclick="rate(4)" src="styles/empty.jpg">
            <img class="img5 star" onclick="rate(5)" src="styles/empty.jpg">
            <h4 style="color: #ee6f57">Click on one of the stars to leave your rating</h4>
            <h4 th:text="${rating.getCount()} + ' user(s) have rated.'"></h4>
        </div>

        </body>
        <script th:inline="javascript">
            /*[+

            const stars = document.querySelectorAll(".star");
            let rating = [[${rating.getRatingAsInt()}]];
            const ratingId = [[${rating.getRatingID().getId().toString()}]];

            +]*/
        </script>
        <script>
            rating = 4;
            for (let i = 1; i <= rating; i++) {
                stars[i - 1].src = "styles/full.jpg";
            }

            function rate(value) {
                document.location.href = ratingId + "/" + value;
            }
        </script>
        </html>


This is the only answer I get from Spring Boot

2020-09-14 10:38:10.327  WARN 17764 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "empty.jpg"]
2020-09-14 10:38:10.327  WARN 17764 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "style.css"]
2020-09-14 10:38:10.380  WARN 17764 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "full.jpg"]
2020-09-14 10:38:33.526  WARN 17764 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "empty.jpg"]
2020-09-14 10:38:33.526  WARN 17764 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "style.css"]
2020-09-14 10:38:33.561  WARN 17764 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "full.jpg"]

I think the problem here is that the route for the used stylesheets and images fits the controller route {ratingId}/{latestRating}. Thatswhy my controller wants string and integer, but gets two strings, eg localhost:8080/styles/style.css when requesting the stylesheet. By using a PostMapping for the mentioned controller route I can fix this problem.

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