I'm trying to send the values of looped checbox for print them in the controller. When I press a button, I want the information of the selected checbox to be send to the controller and print it in the console, but when I press my button to send the information to the controller method, it throws me a NullPointerException. My HTML look like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Select Cities</title>
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row align-items-center">
<div class="col-4 mt-3">
<form action="">
<div class="form-check" th:each="names : ${namesOfCities}">
<label for="" class="form-check-label">
<input type="checkbox" name="lista" th:value="${names}" th:text="${names}">
</label>
</div>
<a href="#"th:href="@{/starbucks/showSelectedCities}" class="btn btn-primary">Aceptar</a>
</form>
</div>
<div class="col-8">
<div id="chart" style="height: 400px"></div>
</div>
</div>
</div>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-3d.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="../js/jquery-3.3.1.min.js"></script>
<script src="../js/popper.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
</body>
</html>
Here in the HTML I create a checkbox for each element in the list sended by the controller.
I have two methods in the Controller, the first one:
@GetMapping("/selectCities")
public ModelAndView selectCities(Model model){
ModelAndView mav = new ModelAndView(ViewConstant.SELECT_CITIES);
//List<String> citiesNames = namesOfCities();
List<String> citiesNames = recortarList();
model.addAttribute("namesOfCities", citiesNames);
return mav;
}
This method shows the HTML view and send a List of String that contains cities names.
The second method looks like this:
@GetMapping("/showSelectedCities")
public ModelAndView showSelectedCities(@RequestParam(name = "lista", required = false)List<String> id){
ModelAndView mav = new ModelAndView(ViewConstant.SELECT_CITIES);
for(int x=0;x<id.size();x++){
System.out.println(id.get(x));
}
return mav;
}
In this I want to recive the names of the city sended by the view and print them in the console, but when I try it throws me a NullPointerException. How is the correct form to send information from a checkbox to the controller?
Aucun commentaire:
Enregistrer un commentaire