Question

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>
Was it helpful?

Solution

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 the Question class would have the following properties:

  • label
  • option1
  • option2
  • option3
  • option4
  • answer

.

public class Question {
    private String label;
    private String option1;
    // ...

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }
    // ...
}

And now, instead of having to iterate on 6 lists at the same time, you can just iterate on the list of questions:

    <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>

Or, with the JSTL tags:

    <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 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 named Question, and not Questions, since every instance of this class represents one question, and not several ones:

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
}

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 it questions and not 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);
 }

And now in the JSP, we can iterate through this list of questions. Inside the loop, the current question will be named "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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top