0

私は現在、JSP、サーブレット、およびいくつかの CRUD データベース サービスを組み込んだ Web サイトに取り組んでいます。目的の効果が得られないため、CRUDの読み取り部分で立ち往生しています。

私はデータベースに接続しており、問題なく Web サイトからデータを挿入できます。私が今やろうとしているのは、データベースからの行の JSP 呼び出しで選択/オプション ボックスを作成し、次のページで行の対応する列を翻訳することです。ただし、オプション ボックスから複数の行を表示することはできません。コードを以下に示します。

豆クラス:

public class ReadBean {
    protected String title;
    protected String author;
    protected String text;

    public String getTitle() {
        return title;
    }

    public void setTitle(String newTitle) {
        title = newTitle;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String newAuthor) {
        author = newAuthor;
    }

    public String getText() {
        return text;
    }

    public void setText(String newText) {
        text = newText;
    }
}

コントローラ:

@WebServlet(urlPatterns = { "/com/defaultValidate/ReadController" })
public class ReadController extends HttpServlet {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        ReadBean reader = new ReadBean();
        String address;

        if (request.getParameter("ReadConfirm") != null) {
            try {
                Connection connect = ConnectionManager.getConnection();
                String SQL = "SELECT * FROM prayers ORDER BY prayerTitle DESC";
                PreparedStatement ps = connect.prepareStatement(SQL);

                ResultSet rs = ps.executeQuery();
                while (rs.next()) {
                    request.getSession().setAttribute("ref", reader);
                    reader.setTitle(rs.getString("prayerTitle"));
                }
            } catch (Exception e) {
            }
            address = "ReadConfirm.jsp";
        } else if (request.getParameter("ReadView") != null) {
            address = "ReadView.jsp";
        } else {
            address = null;
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}

そして、コントローラーが応答する対応する jsp ページ:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@include file="MenuHeader.jsp"%>
</div>
    <div id="mainBorder"> </div>
        <div id="mainBody">
            <table id="mainTable">
                <tr>
                    <td id="sidebar">
                        <h2>Options</h2>  
                            <ul>

            </ul>
        </td>
        <td id="mainContent">
                        <h2 id="contentHeader">Select a prayer</h2>

                        <form action="ReadController">
                            <table border="1" width="1" cellspacing="5" cellpadding="5">
                                <thead>
                                    <tr>
                                        <th></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td><select name="prayer_id">

                                                <c:forEach var="ReadController"       items="${ref.title}">
                                                    <option value="">${ref.title}</option>
                                                </c:forEach>

                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><input type="submit" name="ReadView" value="View">  </td>
                                    </tr>
                                </tbody>
                            </table>

                        </form>
    <td id="imageBar">

            </td>   
        </table>
    <%@ include file="../menuHeaderAndFooter/MenuFooter.jsp" %>

私は間違いなく専門家ではありませんが、コントローラーから JSP の for-each 要素への接続の間に何かが欠けていると思います。興味深いことに、このセクションの WEB-XML 部分は操作可能であり、ConnectionManager はデータベースへの接続をセットアップするための単純な呼び出しです。Glassfish パッケージは使用されるサーバーです。

コードの他の部分について質問がある場合は、この特定のプロジェクトをできるだけ早く効率的に完了して実行することに非常に関心があるので、お知らせください。追加のコーディングや参照が必要な場合は、喜んで提供いたします。

この特定の場所での支援は、非常に高く評価され、非常に役立ちます。ご覧いただきありがとうございます。

4

2 に答える 2

2

問題はコードのこの部分にあります

while(rs.next()){
   request.getSession().setAttribute("ref", reader);
   reader.setTitle(rs.getString("prayerTitle"));
}

新しい値を読み取り、その「ref」をセッションに入れるたびに「ref」属性をオーバーライドしています。したがって、セッションに単一の値を入れているため、単一の行を取得しています。

したがって、代わりにすべての値をリストのようなコレクション内に配置し、そのリストをセッション内に設定します。

List<ReadBean> myList = new Arraylist<ReadBean>();
while(rs.next()){
   ReadBean reader = new ReadBean();   
   reader.setTitle(rs.getString("prayerTitle"));
   myList.add(reader);
}
request.getSession().setAttribute("ref", myList);

for eachこれで、jspの内部を次のように使用できます。

<c:forEach var="myReader" items="${ref}">
       <option value="">${myReader.title}</option>
</c:forEach>

for-each上記のループで行った変更に注意してください。

最初に属性でコレクションが必要なので、セッション内で名前がitems付けられたコレクションを設定して、それを使用できるようにします。ref

var属性で、次のようなローカル変数を定義します

for(Reader myReader : ref)//myReader が入りvarrefリスト (コレクション) が入りますitems

myReaderこれで、varなどを使用してプロパティにアクセスできるfor-eachようになりました。${myReader.title}${myReader['title']}

于 2013-01-30T06:43:37.660 に答える
1

コードを変更する

List<ReadBean> readerList = new Arraylist<ReadBean>();


while(rs.next()){
// blah blah
reader.setTitle(rs.getString("prayerTitle"));
readerList.add(reader); // add this reader instance to list
}
// after while loop
request.getSession().setAttribute("ref", readerList);

それに応じてJSPを変更して、必要に応じてreaderListを反復処理します.jspを変更しなくても機能するはずです。

于 2013-01-30T06:34:31.920 に答える