Digamos, por ejemplo, que tengo una variable b y c en:
<script th:inline="javascript"> let c = 20; let b = 30; document.getElementById("b").innerHTML = b; document.getElementById("c").innerHTML = c; </script>Cuando intento imprimirlos en mi página con:
<h1>"The value for b is: " <span id="b"></span></h1> <h1>"The value for c is: " <span id="c"></span></h1>Esto funciona y los valores de estas variables se muestran en la página. Ahora que estoy usando Thymeleaf y SPring, tengo un método de controlador como este:
@GetMapping("/test") public String test( double val, double val2, @RequestParam double rand, Model model) { model.addAttribute("val", val); model.addAttribute("val2", val2); model.addAttribute("rand", rand); model.addAttribute("distance", distance); System.out.println(val); System.out.println(val2); return "/Test"; }Quiero ingresar solo el rand, val y val2 deben tener los valores de las variables de JavaScript (b y c):
<form th:action="@{/test}" method="get"><br> <input type="text" id="b" th:name="val" " th:value="${b}"> <input type="text" id="c" th:name="val2" " th:value="${c}"> <label for="rand">rand:</label> <input type="number" id="rand" th:name="rand" placeholder="rand" th:default="0"><br> <input type="submit" value="Submit" onclick="add()"> <input type="reset" value="Reset"> </form>Pero no obtengo los valores correctos del código JavaScript. Por lo tanto, quiero asignar las variables b y c a las variables val y val2 para que solo deba ingresar el parámetro de solicitud rand. ¿Alguien tiene una solución para esto? Lo busqué y todos los casos fueron que querían asignar la variable Thymeleaf en el script y no al revés. Gracias por adelantado