dimanche 9 avril 2023

Cant display selected objects with checkbox in Spring boot application

Hello my friends I have a problem with my Spring boot application, When I select activities in my add_activities layout and hit the submit button it doesn't display the selected items in my activities layout, So here is my controller

@Controller
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    public ActivityController(ActivityService activityService) {
        super();
        this.activityService = activityService;
   }

    @GetMapping("/activities")
    public String listSelectedActivities(Model model){
        List<Activity> selectedActivities = (List<Activity>) model.getAttribute("selectedActivities");
        model.addAttribute("selectedActivities", selectedActivities);
        return "activities";
    }

    @PostMapping("/activities/new")
    public String listActivities(@RequestParam(value = "selectedActivities", required = false) List<Long> selectedActivityIds, Model model){
        List<Activity> selectedActivities = new ArrayList<>();
        if (selectedActivityIds != null) {
            for (Long id : selectedActivityIds) {
                selectedActivities.add(activityService.getActivityById(id));
            }
        }
        model.addAttribute("selectedActivities", selectedActivities);
        return "activities";
    }

    @GetMapping("/activities/new")
    public String createActivityForm(Model model, @RequestParam(name = "place", required = false) String place, @RequestParam(name = "name", required = false) String name){
        List<Activity> activities = activityService.getAllActivities();
        if (place != null && !place.isEmpty()) {
            activities = activities.stream()
                    .filter(a -> a.getPlace().toLowerCase().contains(place.toLowerCase()))
                    .collect(Collectors.toList());
        }

I was expecting to see selected activites in my activities layout not an empty table Thank you for helping me




Aucun commentaire:

Enregistrer un commentaire