I'm new to struts and recently heard that using jstl tags is the most preferred way but I'm having a tough time getting through.
Questions.java
public class Questions {
private String label;
private String option1;
....
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
...
}
This is my action class
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);
}
Now Netbeans show all the statements in while loop with errors :
no suitable method found for add(String) method List.add(int,Questions) is not applicable (actual and formal argument lists differ in length) method List.add(Questions) is not applicable (actual argument String cannot be converted to Questions by method invocation conversion)
I'm trying to get this data in my jsp page using the jstl tags. This is the page its forwaded to
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>
First of all, you're not using any JSTL tag in your JSP. You're using Struts tags. You should indeed prefer JSTL tags.
Now, your problem is a design problem. Instead of having 6 lists of String, you should have a single list of
Question
instances, where theQuestion
class would have the following properties:.
And now, instead of having to iterate on 6 lists at the same time, you can just iterate on the list of questions:
Or, with the JSTL tags:
Java is an object-oriented language. Use objects.
Also, consider moving away from Struts1, which is an obsolete, abandoned framework.
EDIT:
OK. So you first need a
Question
class. The class should be namedQuestion
, and notQuestions
, since every instance of this class represents one question, and not several ones:Now, when reading rows from your table, you should create one
Question
object per row, and fill a list of questions. As the list contains several questions, we'll name itquestions
and notquestion
:And now in the JSP, we can iterate through this list of questions. Inside the loop, the current question will be named "question".