0

我的 jsp 页面有问题(我是新手)。在一个jsp页面中,我写了一个从数据库中读取的表。我在这里写页面的代码:

<%@page import="java.sql.SQLException"%>
<%@page import="java.lang.String"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.util.Date "%>
<%@page import="jord.ConnectionManager "%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% 
    String sql= "select numelencotrasm,dtelencotrasm \n"+
                "from fimand \n"+
                "where eser=2012 \n"+
                "group by numelencotrasm,dtelencotrasm \n"+
                "order by numelencotrasm";
    ConnectionManager manager = new ConnectionManager();
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try{
        conn = manager.getConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
    }catch(SQLException ex){
        rs.close();
        conn.close();
        rs=null;
        conn=null;
        response.sendRedirect("../index.jsp");
    }
%>
<form id="tabella1" name="tabella1">
    <input name="numelencotrasm" type="hidden" value=""/>
    <input name="dtelencotrasm" type="hidden" value=""/>
    <table width="275px" cellspacing="0" cellpadding="1" border="1">
        <tr>
            <td class="TabellaTitolo" width="50%">
                Numero Distinta
            </td>
            <td class="TabellaTitolo" width="50%">
                Data Distinta
            </td>
            <td class="TabellaTitolo" width="50%">
                #
            </td>
        </tr>
        <%
        try{
            while(rs.next()){
        %>
        <tr>
            <td>
                <input type="text" value="<% out.print(rs.getInt(1)); %>"/>
            </td>
            <td>
                <input type="text" value="<% out.print(rs.getDate(2)); %>"/>
            </td>
            <td>
                <a href="#" onclick="javascript:setValue('<% out.print(rs.getInt(1)); %>','<% out.print(rs.getDate(2));%>');">
                    Click-me
                </a>
            </td>
        </tr>
        <%
            }
        }catch(Exception e){
            response.sendRedirect("../index.jsp");
        }
        %>
    </table>
</form>
<script language="Javascript">


    function setValue(num, dt){
        document.tabella1.numelencotrasm.value=num;
        document.tabella1.dtelencotrasm.value=dt;
        alert(document.tabella1.numelencotrasm.value); //test
        alert(document.tabella1.dtelencotrasm.value);//test
    }
</script>

好吧..我现在要创建一个新的html-table 点击行“#”从数据库中读取数据并传递所选行的值。

我为基本的英语道歉!:/

4

1 回答 1

0

您需要在上面的 jsp 代码中进行以下更改:

  1. 将操作 URL 添加到您的表单。
  2. 在调用 setValue() 之后调用提交表单。

在动作jsp中:

  1. 抓取上面页面提交的参数,
  2. 进一步处理它们以获取数据库中的详细数据,
  3. 构建一个 html 表格,并将其填充以显示。

这是一个简单的解决方案,认为您刚刚开始学习。

更新 1

I want to display an iframe that contains the date table2 on click of the table1.

For this you need following changes in your current JSP.

  1. Define action URL as another JSP. It shall be something like action="targetPage.jsp".
  2. Define target attribute value for the form as your iframe. It shall be like target="nameOfTheIframe".
  3. Define and place an iframe in the page as appropriately. It shall be similar to <iframe name="targetIframe" src="about:blank"></iframe>.
  4. In the JavaScript setValue function write instructions to submit the form.
  5. In your other JSP, receive parameters submitted by this form, process them and display result.
于 2012-05-26T16:11:05.850 回答