2012-05-26 244 views
0

我有一个jsp页面的问题(我是新手)。 在一个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表点击“#”从通过选定的数值数据库中读取数据行。

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

+0

建议不要在jsp中使用与servlet相关的代码。 – sudmong

回答

0

您需要按照上面的JSP代码的变化:

  1. 添加动作URL到表单中。
  2. 在调用setValue()之后,调用提交表单。

在操作的jsp:

  1. 捕获提交上述页面的参数,从数据库的详细数据
  2. 工艺对他们进一步,
  3. 构造一个HTML表格,并填写它来显示。

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

更新1

我想显示包含在表1中点击日期表2的iframe中。

为此,您需要对当前JSP进行以下更改。

  1. 将动作URL定义为另一个JSP。它应该是类似于action="targetPage.jsp"
  2. 将表单的目标属性值定义为iframe。它 应该像target="nameOfTheIframe"
  3. 定义并在页面中正确放置iframe。它应该类似于<iframe name="targetIframe" src="about:blank"></iframe>
  4. 在JavaScript setValue函数中写入指令来提交表单。
  5. 在您的其他JSP中,接收由此表单提交的参数 处理它们并显示结果。
+0

但是,如果我在表单上提交将重新加载页面。 我想显示一个包含日期table2的iframe单击table1。这是可能的? – user1418910

+0

@ user1418910是的,这是可能的。检查我更新的答案。 –

+0

谢谢!你的回答非常有帮助。 这是我在StackOverflow上的第一个问题,我不知道是否应该采取措施来解决问题。 – user1418910