java 将对象绑定到 JSP 页面上的控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10396513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding objects to controls on JSP pages
提问by Ray
I have the following class that I'm using in my Java with JSP applicaton.
我在带有 JSP 应用程序的 Java 中使用了以下类。
// public class QuestionBO implements Serializable{
// 公共类 QuestionBO 实现了可序列化{
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.
JSP 页面有一个表单,其中每个问题(其字符串表示)旁边都有一个复选框。用户标记一些问题并将表单提交给服务器以供 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?
我如何从这个页面上的 servlet 获取地图?
采纳答案by BalusC
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:
这样,您只需在 servlet 中执行以下调用即可获取所有已检查的值:
String[] chosenQuestions = request.getParameterValues("chosen_question");
回答by Kapelchik
Generate an unique name for each checkbox
as follows:
为每个生成一个唯一的名称,checkbox
如下所示:
<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
之后,您已经能够checkbox
在servlet 中识别每个