javaspring-tool-suite

java page filter problems


@GetMapping("/filter/{talla}/{tipo}/{marca}/{palabra}")
    public String busqueda(Model model,
                           @PathVariable(name = "talla", required = false) String talla,
                           @PathVariable(name = "tipo", required = false) String tipo,
                           @PathVariable(name = "marca", required = false) String marca,
                           @PathVariable(name = "palabra", required = false) String palabra) {

        List<Zapatilla> zapatillas = servi.todasZapatillas();

        // Verificar si algún parámetro tiene un valor y aplicar el filtro correspondiente
        if (talla != null) {
            zapatillas = servi.filtrarPorTalla(zapatillas, talla);
        }

        if (tipo != null) {
            zapatillas = servi.filtrarPorTipo(zapatillas, tipo);
        }

        if (marca != null) {
            zapatillas = servi.filtrarPorMarca(zapatillas, marca);
        }

        if (palabra != null) {
            zapatillas = servi.buscarPorNombre(zapatillas, palabra);
        }
        
        model.addAttribute("zapatillas", zapatillas);
        return "shoes.jsp";
    } 

in theory this has to work with each filter but only works if i put every single one otherwise it give me a 404 error and i dont know why

i tried solving it with changing estructure checking routes but nothing works


Solution

  • If you want to filter data then user Query param instead of path variable.

    @GetMapping("/filter")
        public String busqueda(Model model,
               @RequestParam(name = "talla", required = false) String talla,
               @RequestParam(name = "tipo", required = false) String tipo,
               @RequestParam(name = "marca", required = false) String marca,
    

    Something like this.

    Your api will be : http://localhost:8080/filter?talla=somevalue&tipo=somevalue

    You can add all the values or skip whatever not present.