Driver returns list with data but in the view it is not displayed (Spring)

1

I am new to this from Spring, I have made an application everything works fine, except that although the list in the controller is full, it does not show the values in the jsp.

@Controller
public class DBUserController {

    @Autowired
    private DBUserService dbuserService;

    @RequestMapping("/")
    public String listDBUser(Map<String, Object> map) {

        map.put("DBUserList", dbuserService.listDBUser());

        return "DBUser";
    }

}

 <c:forEach items="${DBUserList}" var="DBUser">
    <tr>
        <td>${DBUser.username}</td>
        <td>${DBUser.createdBy}</td>
    </tr>
 </c:forEach>
    
asked by Margie Giselle Orellano Cassia 15.08.2018 в 21:52
source

1 answer

0

First of all, remember to put this in the section head te tu jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

and add as dependency https://mvnrepository.com/artifact/javax.servlet/jstl/1.2 .

Then there are several options.

Option 1 :

In public String listDBUser(Map<String, Object> map) you do not receive an object of type Map<String, Object> as an argument, but ModelMap or Model .

@Controller
public class DBUserController {

    @Autowired
    private DBUserService dbuserService;

    @RequestMapping("/")
    public String listDBUser(ModelMap map) {  // <-- recibir argumento de tipo ModelMap

        map.addAttribute("DBUserList", dbuserService.listDBUser()); // <-- adicionar la lista en el atributo DBUserList

        return "DBUser";  // <-- suponinendo que "DBUser" es el nombre de tu vista
    }

}

Option 2 :

  • You do not return String of method listDBUser , but ModelAndView
  • You do not need to send a Map as a parameter to the listDBUser method (service in url / ).
  • driver

    @Controller
    public class DBUserController {
    
        @Autowired
        private DBUserService dbuserService;
    
        @RequestMapping("/")
        public ModelAndView listDBUser() {  // <-- quitar argumento aqui y cambiar retorno a ModelAndView
    
            // crear objeto ModelAndView para retornar
            ModelAndView modelAndView = new ModelAndView("DBUser"); // <-- suponinendo que "DBUser" es el nombre de tu vista
            modelAndView.addObject("DBUserList", dbuserService.listDBUser());
    
            return modelAndView;
        }
    
    }
    

    This excellent tutorial explains these concepts.

        
    answered by 16.08.2018 / 15:23
    source