Binding objects to controls on JSP pages

时光总嘲笑我的痴心妄想 提交于 2020-01-04 05:33:09

问题


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?


回答1:


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");



回答2:


Generate an unique name for each checkbox as follows:

<input type="checkbox" name="${question.questionId}" />

or:

<input type="checkbox" name="choosen_question_${question.questionId}" />

After that, you're already able to identify each checkbox in your servlet



来源:https://stackoverflow.com/questions/10396513/binding-objects-to-controls-on-jsp-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!