Popular dropdown list with database in Spring MVC Controller

1

I'm having a hard time finding a way to populate a dropdown on the Controller and move on to the view. Because this way I'm doing when I enter the screen to update the dropdown does not return with the value it already was.

I'm doing this (controller):

@RequestMapping(value = { "/updateAgenda/{idAgenda}" }, method = RequestMethod.GET)
public ModelAndView update(@PathVariable("idAgenda") Integer idAgenda, Model atributos) {
    ModelAndView model = new ModelAndView("agenda/atualiza-agenda"); 

    Agenda agenda = agendaService.buscarAgendaPorId(idAgenda); 

    model.addObject("agenda", agenda); 

    atributos.addAttribute("destinos", destinoService.buscarTodosDestinos()); 
    atributos.addAttribute("rotas", rotaService.buscarTodasRotas()); 

    return model; 
}

And in the view:

<form:select path="idDestinoAgendamento" class="form-control">
    <option value="-1">Selecione o Destino</option>
    <c:forEach items="${destinos}" var="destino">
        <option value="${destino.idDestino}">${destino.nomeLocalDestino}</option>
    </c:forEach>
</form:select>

My problem is that when I do a POST request on the page my dropdown loads again, I can not retrieve the value it was.

    
asked by anonymous 26.07.2017 / 16:19

1 answer

1

It has been resolved. I'm using the same controller, just modified the view:

<form:select path="idDestinosd" class="form-control">
    <option value="-1">Selecione o Destinos</option>
    <c:forEach items="${destinos}" var="destino">
        <c:choose>
            <c:when test="${destino.idDestinosd eq agenda.destinosd.idDestino}">
                <option value="${destino.idDestino}" selected="true">${destino.nomeLocalDestino}</option>
            </c:when>
            <c:otherwise>
                <option value="${destino.idDestino}">${destino.nomeLocalDestino}</option>
            </c:otherwise>
        </c:choose>
    </c:forEach>
</form:select>
    
26.07.2017 / 18:39