Popular dropdown list with Spring MVC Controller database

Asked

Viewed 323 times

1

I’m having trouble finding a way to popular a dropdown in 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 that was already.

I’m doing it this way (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 make a POST request on the page my dropdown loads again, I am unable to recover the value I was.

1 answer

1

It’s solved. I’m using the same controller, I 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.