0

我昨晚问了这个问题,不问如何完成它的编码有点愚蠢。我现在在 JSP 中有一个下拉框,其中填充了来自 SQL 数据库的一些数据。我正在尝试将所选数据定向到最终查看页面,但再次遇到了一些困难。

当我运行程序时,它要么使用列表数据库中的所有数据填充页面,要么会给我一个不友好的错误。

我目前的编码是这样的:

豆子:

package com.login.read;

public class ReadBean {

protected int id;
protected String title;
protected String author;
protected String text;


public int getId(){
    return id;
}

public void setId(int newID){
    id = newID;
}

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

}

控制器:

package com.login.read;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


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




protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String address;
    List<ReadBean> myList = new ArrayList<ReadBean>();

    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()){
                ReadBean reader = new ReadBean();
                reader.setTitle(rs.getString("prayerTitle"));
                reader.setAuthor(rs.getString("author"));
                reader.setText(rs.getString("text"));
                myList.add(reader);   
            }

            request.getSession().setAttribute("ref", myList);


            rs.close();
            ps.close();
            connect.close();
        } 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="view" items="${ref}">
                            <h2>${view.title}</h2>

                            <h3>${view.author}</h3>

                            <p>${view.text}</p>
                        </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" %>

和最后的查看页面:

 <%@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">


                        <c:forEach var="view" items="${res}">
                            <h2>${view.title}</h2>

                            <h3>${view.author}</h3>

                            <p>${view.text}</p>
                        </c:forEach>




                        <form action="../Read.jsp">
                            <p>Click on the return button to return to the main menu</p>
                            <input type="submit" name="return" value="Return">
                        </form>

    <td id="imageBar">

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

与往常一样,这方面的任何和所有帮助都非常有帮助。我是 jsp 和 servlet 领域的新手。

如果需要任何关于此序列延续的参考,请查看我发布的关于在 jsp 组合框中动态显示数据的上一个问题。

感谢您的时间和帮助。

4

1 回答 1

0

通过阅读上面的代码,我可以问您将会话属性存储在变量“var”中的位置,该变量在c:foreach循环中使用?

于 2013-01-31T06:02:47.870 回答