我是struts的新手,最近听说使用jstl标签是最受欢迎的方式,但我很难度过。
Questions.java
public class Questions {
private String label;
private String option1;
....
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
...
}
这是我的动作类
PaperEdit val = (PaperEdit)form;
String sql = "SELECT * FROM "+val.getCategory();
List<Questions> question = new ArrayList<Questions>();
try{
Statement st = DBConnection.DBConnection.DBConnect();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
question.add(rs.getString("ques_name"));
question.add(rs.getString(3));
question.add(rs.getString(4));
question.add(rs.getString(5));
question.add(rs.getString(6));
question.add(rs.getString(7));
}
request.setAttribute("ques", question);
}
现在Netbeans显示while循环中包含错误的所有语句:
没有为add(String)找到合适的方法 方法List.add(int,Questions)不适用 (实际和正式的参数列表长度不同) 方法List.add(Questions)不适用 (实际参数String不能通过方法调用转换转换为Questions)
我正在尝试使用jstl标签在我的jsp页面中获取此数据。这是其转发的页面
display.jsp
<table width="60%" align="center" border="1px">
<logic:iterate name="ques" id="question">
<tr>
<td><bean:write name="question" property="ques_name"/></td>
</tr>
</logic:iterate>
</table>
答案 0 :(得分:0)
首先,您没有在JSP中使用任何JSTL标记。你正在使用Struts标签。你应该更喜欢JSTL标签。
现在,您的问题是一个设计问题。您应该拥有一个Question
个实例列表,而不是拥有6个String列表,Question
类具有以下属性:
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
课程。该类应命名为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
个对象,并填写一个问题列表。由于该列表包含多个问题,因此我们将其命名为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中,我们可以遍历这个问题列表。在循环内部,当前问题将被命名为“问题”。
<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>