简体   繁体   中英

How to get element of dropdown list from JSP

I have one User class,

//UserDAO
private String name;
private String[] certifications;

//getters and setters here....

Controller.java

List<UserDAO> usersList= new ArrayList<UserDAO>();
UserDAO userOne = new UserDAO();
userOne.setName("user1");
userOne.setCertifications({"cert1"});

UserDAO userTwo = new UserDAO();
userTwo.setName("user1");
userTwo.setCertifications({"cert1","cert2"});

usersList.add(userOne);
usersList.add(userTwo);

model.addAttribute("usersList", usersList);

JSP

<c:forEach var="list" items="${usersList}">
    <tr>
        <td>"${list.name}"/>
        <input type="hidden" name="name" value="${list.name}"/></td>
        <td>
        <select>
        <c:forEach var="rowSelect" items="${list.certifications}">  
        <option value = "${rowItemSelect}">${rowItemSelect}</option>    
        </c:forEach>                                                                                                    
        </select>
        </td>
    </tr>
</c:forEach>

Above code populates data correctly. Please tell how to send the selected dropdown value for certification in JSP and how to access these values in controller.

You have to place your select dropdown in a <form> and when the form is submitted the corresponding setter should be called.

You have to give a name to your <select> tag so that the framework knows which variable to assign the <option> value to. If you want to send the value back to myName variable then your <select> should look like this

<select name="myName">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM