0

私は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>
4

1 に答える 1

0

まず、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>
于 2013-11-30T20:46:50.817 に答える