2012-05-26 148 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