I see this question with hundreds of answers, none of which have worked for me yet. I am building a simple asset tracker. My controller has two functions
@Controller
public class GpsController {
private double bestLat = 0.0;
private double bestLon = 0.0;
private double divisor = 10000000.0;
@GetMapping("/map")
public String map(Model model) {
model.addAttribute("latitude", bestLat);
model.addAttribute("longitude", bestLon);
return "map";
}
@PostMapping("/addlocation")
public void addLocation(@RequestBody GpsMessage message) {
if(message.getGpsTimestamp() == 0L) {
bestLat = message.getTriLat();
bestLon = message.getTriLon();
} else {
bestLat = message.getGpsLat() / divisor;
bestLon = message.getGpsLon() / divisor;
}
DbConnection.writeLocation(message);
}
}
I'm using Thymeleaf in /map to pass the latitude and longitude into the JS for the Google Maps API. The program works just fine, but I get these errors from Thymeleaf. I could ignore it but I prefer to find what is causing it.
2022-06-13 05:18:14.177 ERROR 28188 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "addlocation": Error resolving template [addlocation], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [addlocation], template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE]*
What confuses me is that it is complaining about the addLocation method when it is not using thymeleaf.
In resources, I have index.html, index.js, and style.css inside the static folder and the map.html file under templates.
map.html looks like this if it matters...
<!DOCTYPE html>
<!--
@license
Copyright 2019 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Add Map</title>
<meta http-equiv="refresh" content="10">
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<script th:inline="javascript">
var myLat = /*[[${latitude}]]*/;
var myLon = /*[[${longitude}]]*/;
</script>
</head>
<body>
<!--The div element for the map -->
<div id="map"></div>
<script
src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&callback=initMap&v=weekly"
defer
></script>
</body>
</html>