まず、JSP で JSTL タグを使用していません。Struts タグを使用しています。JSTL タグを使用することをお勧めします。
さて、あなたの問題は設計上の問題です。String のリストを 6 つ持つ代わりに、Question
インスタンスのリストを 1 つ持つ必要があります。Question
クラスには次のプロパティがあります。
- ラベル
- オプション1
- オプション2
- オプション3
- オプション4
- 答え
.
public class Question {
private String label;
private String option1;
// ...
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
// ...
}
これで、同時に 6 つのリストを反復する代わりに、質問のリストを反復することができます。
<logic:iterate name="questions" id="question">
<tr>
<td><bean:write name="question" property="label"/></td>
<td><bean:write name="question" property="option1"/></td>
...
</tr>
</logic:iterate>
または、JSTL タグを使用すると、次のようになります。
<c:forEach var="question" items="${questions}">
<tr>
<td><c:out value="${question.label}" /></td>
<td><c:out value="${question.option1}"/></td>
...
</tr>
</c:forEach>
Java はオブジェクト指向言語です。オブジェクトを使用します。
また、廃止され放棄されたフレームワークである Struts1 からの移行を検討してください。
編集:
わかった。Question
したがって、最初にクラスが必要です。このクラスのすべてのインスタンスは、複数の質問ではなく1 つのQuestion
質問を表すため、クラスにはではなくという名前を付ける必要があります。Questions
public class Question {
private String label;
private String option1;
// other fields omitted for brevity
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
// other getters and setters omitted for brevity
}
ここで、テーブルから行を読み取るときは、Question
行ごとに 1 つのオブジェクトを作成し、質問のリストを入力する必要があります。questions
リストにはいくつかの質問が含まれているため、名前を付けますquestion
。
// this is an list of questions. It's empty initially
// this list isn't meant to contain Strings, and it can't.
// It will contain one Question object for each row in the table.
List<Question> questions = new ArrayList<Question>();
try{
Statement st = DBConnection.DBConnection.DBConnect();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
// this block is executed for each row in the table.
// Each row is transformed into a Question object
Question question = new Question();
question.setLabel((rs.getString("ques_name"));
question.setOption1(rs.getString(3));
question.setOption2(rs.getString(4));
question.setOption3(rs.getString(5));
question.setOption4(rs.getString(6));
question.setAnswer(rs.getString(7));
// now that we have created a Question object and populated it with the
// cells of the row, we will add it to the list of questions:
questions.add(question);
}
// So now, we have a list of questions. Each element of the list is an object
// of type Question, which has a property label, a property option1, etc.
request.setAttribute("questions", questions);
}
そして今、JSP で、この質問のリストを繰り返すことができます。ループ内では、現在の質問に「question」という名前が付けられます。
<logic:iterate name="questions" id="question">
<tr>
<%-- let's write the property label of the current question
This will in fact call question.getLabel() and write it to the response
--%>
<td><bean:write name="question" property="label"/></td>
<%-- let's write the property option1 of the current question
This will in fact call question.getOption1() and write it to the response
--%>
<td><bean:write name="question" property="option1"/></td>
</tr>
</logic:iterate>