'Need to access/send var data from JavaScript to Java Spring Boot Controller ThymeLeaf
I'm still learning JS, Thymeleaf, and Spring Boot and can't seem to figure out how I can use data variables in my JavaScript file and send them to a database through Spring MVC controller. Here is what I have so far:
html:
<body>
<!--PRINT OUT TEST_VAR HERE FOR USERS TO SEE DATA-->
<script src="../static/js/TEST.js" type="text/javascript" th:src="@{/js/TEST.js}"></script>
</body>
JavaScript:
var TEST_VAR = 37;
Java/Spring MVC Controller:
@RequestMapping(value = "TEST_VAR", method = RequestMethod.GET)
public String messages(Model model) {
model.addAttribute("test_db", "test_var");
return "TESTindex";
// Not sure what to write in the controller.
}
Solution 1:[1]
in a SpringBoot Controller, the "value" in the requestmap annotation is where the end point is binded to
so the example below binds to "abc.com/api/v1/17" and 17 is the variable that will be readed from path (because if you use GET method, you need to read data from path) Get methods doesn't have request body (investigate GET and POST method diffrences, if you need )
@RequestMapping(value = "/api/v1/{id}", method = GET)
public String getFoosBySimplePathWithPathVariable(
@PathVariable("id") String id) {
return "you send id = " + id;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gsahan |
