LoginSignup
0
0

More than 3 years have passed since last update.

[うまくいかなかった]Listの中身が表示されない問題

Posted at

状況

1.top.jsp
2.TestServlet
3.test.jsp ←イマココ
4.TestResultServlet
5.testResult.jsp
6.top.jsp

配列を用意してsetAttributeでServletで渡ってるはずなのに、リストデータを取得した以下の文の中身がJSPに表示されない。

//test.jsp

//リストデータをリクエストから取得
List<QuestionsBean> qlist = (List<QuestionsBean>)request.getAttribute("qlist");

    //questionのデータが空でない場合
    if(qlist != null){

        //qestionのデータの数分、繰り返し処理
        for(int i=0;i<qlist.size();i++){
%>

<div class="inputQuestion">
    <p>
        <!-- 問題番号 -->
        <input readonly type="text" name="questionId" value="<%= qlist.get(i).getId() %>">
    </p>
    <p>
        <!-- 問題 -->
        <textarea readonly name="question"><%= qlist.get(i).getQuestion() %></textarea>
    </p>
</div>

<!-- 回答 -->
<div class="input-answer">
<p> 回答:
    <!-- 答え -->
        <input type="text" name="answer" id="answer">
</p>
</div>
<%
    }
}
%

原因

①サーブレットの遷移先が間違っていた:baby_tone1:

//TestServlet.java

RequestDispatcher rd = request.getRequestDispatcher("list.jsp");
            rd.forward(request, response);

コピペマンのあるある。
そのせいでサーブレットからデータが渡ってきてなかった

②postに渡したいのにaタグで書いてた

//top.jsp
誤 <a href="TestServlet">テストをする</a>
//top.jsp
正 <form action="TestServlet" method="post">
        <input type="submit" value="テストをする">
    </form>

まとめ

遷移先のミスは想定外でした。。
2回目のミスなので備忘録:boy_tone1:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0