JSP how to include a select inside a checkbox

0

I'm developing a web application with java spring mvc , and I need to list items in a dynamic checkboxList and include a select option on each item.

Something that in html would be similar to the code below:

<ul>
    <li>
        <input type="checkbox"> Item 1 - 
            <select>
                <option value="1">Ativo</option>
                <option value="2">Inativo</option>
                <option value="3">Bloqueado</option>
            </select>
    </li>
    <li>
        <input type="checkbox"> Item 2 - 
            <select>
                <option value="1">Ativo</option>
                <option value="2">Inativo</option>
                <option value="3">Bloqueado</option>
            </select>
    </li>
    <li>
        <input type="checkbox"> Item 3 - 
            <select>
                <option value="1">Ativo</option>
                <option value="2">Inativo</option>
                <option value="3">Bloqueado</option>
            </select>
    </li>
</ul>

How can I dynamically do this in jsp? Remembering that the items in the checkbox come from the database.

    
asked by anonymous 10.04.2016 / 17:10

2 answers

1

If you are using JSP with JSTL, add it to the beginning of the file:

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

And in every place you need to select, put

<select>
<c:forEach var="item" items="${algumBean.listaDeAlgumaCoisa}">
  <option value="${fn:escapeXml(item.valor)}">${fn:escapeXml(item.descricao)}</option>
</c:forEach>
</select>

listaDeAlgumaCoisa should be a list of objects where each contains a field called valor and a field called descricao . How to make the object algumBean available to the page depends a bit on the framework, and I've never done any project with Spring, but it should not be difficult.

Remembering that with Java and Spring it is possible to use several template languages, such as JSP (with JSTL and other tag libraries ), Thymeleaf, FreeMarker, etc. As your example is pure HTML, I could not check what you are using, I made the example using only JSP with JSTL.

    
14.04.2016 / 22:47
1

Speak Erico. All blaze?

Well, on a JSP page you can use the open and close tags of java code inside a JSP page. EX:

            <select>
                <%
                   List<String> resultado = ClasseDAO.pegarResultado();
                   for(String valor : resultado ){
                     out.println("<option value=\"" + valor + "\" >" + valor  + "</option>");
                   }
                 %>
            </select>
    
14.04.2016 / 22:11