Java Thymeleaf和弹簧靴导致类型不匹配异常
在使用thymeleaf时,我在单击其中一幅图像时突然出现以下错误:Java Thymeleaf和弹簧靴导致类型不匹配异常,java,spring,spring-boot,thymeleaf,Java,Spring,Spring Boot,Thymeleaf,在使用thymeleaf时,我在单击其中一幅图像时突然出现以下错误: 已解决[org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:未能将类型“java.lang.String”的值转换为所需类型“int”;嵌套异常为java.lang.NumberFormatException:用于输入字符串:“style.css” 我试图用标准值来消除这个标准错误,但没有帮助。有人知道这个错误是从哪里来的吗
已解决[org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:未能将类型“java.lang.String”的值转换为所需类型“int”;嵌套异常为java.lang.NumberFormatException:用于输入字符串:“style.css”
我试图用标准值来消除这个标准错误,但没有帮助。有人知道这个错误是从哪里来的吗
信息:此错误导致我的文件“style/style.css”“style/full.jpg”“style/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>
我认为这里的问题是,所用样式表和图像的路由适合控制器路由{ratingId}/{latestRating}。这就是为什么我的控制器需要字符串和整数,但在请求样式表时会得到两个字符串,例如localhost:8080/styles/style.css。通过对所提到的控制器路由使用PostMapping,我可以解决这个问题。在堆栈跟踪中可以看到错误的来源。您可能有一个映射捕捉到了这一点(可能是路径映射)。您说当您单击其中一个图像时会发生错误。因此,您必须在onclick=“rate(5)”处检查rate方法。其中一个图像正在发送一个无法转换为数字的字符串(例如“style.css”)。还向我们展示rate方法。函数rate(value){document.location.href=ratingId+“/”+value;}做得不多您可以为此放置github项目吗?这样就可以复制?我能看出我的观察是正确的,解决方案最终解决了它
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"]