Binding objects to controls on JSP pages
Give each checkbox an unique value. For example, the unique question identifier:
<c:forEach items="${questionsForSubject}" var="question">
<tr>
<td>
<input type="checkbox" name="chosen_question" value="${question.questionId}" />
${question.question}
<br />
</td>
</tr>
</c:forEach>
This way you'll be able to grab all checked values by just the following call in the servlet:
String[] chosenQuestions = request.getParameterValues("chosen_question");
Ray
Updated on June 04, 2022Comments
-
Ray almost 2 years
I have the following class that I'm using in my Java with JSP applicaton.
// public class QuestionBO implements Serializable{
private int questionId; private int testID; private String question; private TutorBO infoAboutTutor; private SubjectBO infoAboutSubject; private TestBO infoAboutTest; private List<AnswerBO> answers; public QuestionBO() { } public QuestionBO(String question) { this.question = question; }
getter & setter....
The JSP page has a form where each Question (its String representation) has a checkbox next to it. A user marks some of the questions and submits the form to the server for processing by a servlet.
What is the conventional way of binding the Question objects with the checkboxes so that I could find out what Questions have been selected?
Currently I'm using the following approach for constructing the form:
//
<c:if test="${not empty questionsForSubject}"> <form action="/TutorWebApp/controller" method="POST" name="addQuestionForm"> <input type="hidden" name="command" value="add_question_list" /> <input type="hidden" name="testName" value="${testName}"/> <table border ="1"> <tbody> <c:forEach items="${questionsForSubject}" var="question"> <tr> <td> <input type="checkbox" name ="choosen_question" value="${question.getQuestion()}"> ${question.getQuestion()} <br /> </td> </tr> </c:forEach> </tbody> </table> <input type="submit" value="Add questions "/> </form>
And I shouldn't use frameworks.
Thanks
And I have last question
<c:if test="${not empty questionsForSubject}"> <form action="/TutorWebApp/controller" method="POST" name="addQuestionForm"> <input type="hidden" name="command" value="add_question_list" /> <input type="hidden" name="testName" value="${testName}"/> <input type="hidden" name="questionsForSubject" value="${questionsForSubject}"/> <table border ="1"> <tbody> <c:forEach items="${questionsForSubject.keySet()}" var="questionID"> <tr> <td> <input type="checkbox" name ="choosen_question" value="${questionID}"> ${questionsForSubject.get(questionID).getQuestion()} <br /> </td> </tr> </c:forEach> </tbody> </table> <input type="submit" value="Добавить вопросы"/> </form>
How I can get map from this page on servlet?