简体   繁体   English

Thymeleaf 和 Spring Boot 导致 TypeMismatchException

[英]Thymeleaf and Spring Boot causing TypeMismatchException

When working with thymeleaf I suddenly got the following error when clicking one of the images:使用 thymeleaf 时,单击其中一张图像时突然出现以下错误:

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.信息:此错误导致我的文件“styles/style.css”“styles/full.jpg”“styles/empty.jpg”无法加载。

<!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这是我从 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}.我认为这里的问题是使用的样式表和图像的路由适合控制器路由 {ratingId}/{latestRating}。 Thatswhy my controller wants string and integer, but gets two strings, eg localhost:8080/styles/style.css when requesting the stylesheet.这就是为什么我的控制器需要字符串和整数,但在请求样式表时得到两个字符串,例如 localhost:8080/styles/style.css。 By using a PostMapping for the mentioned controller route I can fix this problem.通过对提到的控制器路由使用 PostMapping,我可以解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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