2014-09-03 49 views
-1

我是JSP的初學者,我試圖用我的數據庫中的一些值填充表格!返回的值是一個包含特殊字符的字符串,在雙重分割後,我嘗試將值傳遞給表!問題是即使我嘗試了什麼都沒有發生,並且我的表仍然是空的,即使這些方法返回了正確的值!問題可能出現在html中,但我找不到任何解決方法!任何想法都會受到歡迎!JSP表格不顯示db的值

我的代碼:

<table id="seller_table" border="1"> 
       <tr> 
        <th>House id </th> 
        <th>Sell</th> 
        <th>Rent</th> 
        <th>Surface </th> 
        <th>Type of Building </th> 
        <th>Public Costs </th> 
        <th>Year of Build/Renovation </th> 
        <th>Type of Heat </th> 
      </tr> 
    <% 

      String line; 
      int counter=0; 
      int i = 0; 
      if(Souli.hoho()!=null){ 
      counter = 0 ; 
      for(i=0;i<Souli.hoho().length();i++){ 
       if(Souli.hoho().charAt(i) == '$'){ 
        counter++; 
       } 
      } 
      String[] lines = Souli.hoho().split("\\$"); 

      for(i=0;i<=counter-1;i++){ 

       line = lines[i]; 
       String[] kati = line.split("#"); 
    %> 




      <tr> 
       <td><%=kati[i]%></td> 
       <td><%=kati[i+2]%></td> 
       <td><%=kati[i+1]%></td> 
       <td><%=kati[i+4]%></td> 
       <td><%=kati[i+3]%></td> 
       <td><%=kati[i+9]%></td> 
       <td><%=kati[i+5]%></td> 
       <td><%=kati[i+6]%></td> 
      </tr> 
      </table> 
<%}}%> 
+0

爲什麼不簡單'for(String line:lines){...}'而不是2個週期?你爲什麼使用'kati [i + 2]',其中'i'是'lines []'數組的索引?那是對的嗎? 'lines []'和'kati []'是不同的數組。 – gooamoko 2014-09-03 13:57:15

+0

我不知道它可以給我一個例子嗎? – gimbo 2014-09-03 13:58:47

+2

[避免使用scriptlets。](https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files?rq=1) – GriffeyDog 2014-09-03 14:03:24

回答

1

試試這個

<table id="seller_table" border="1"> 
       <tr> 
        <th>House id </th> 
        <th>Sell</th> 
        <th>Rent</th> 
        <th>Surface </th> 
        <th>Type of Building </th> 
        <th>Public Costs </th> 
        <th>Year of Build/Renovation </th> 
        <th>Type of Heat </th> 
      </tr> 
    <% 

      //int i = 0; do you need i? I don't understand original structure 
      if(Souli.hoho()!=null){ 
      String[] lines = Souli.hoho().split("\\$"); 

      for(String line: lines){ 
       String[] kati = line.split("#"); 
    %> 




      <tr> 
       <td><%=kati[0]%></td> 
       <td><%=kati[2]%></td> 
       <td><%=kati[1]%></td> 
       <td><%=kati[4]%></td> 
       <td><%=kati[3]%></td> 
       <td><%=kati[9]%></td> 
       <td><%=kati[5]%></td> 
       <td><%=kati[6]%></td> 
      </tr> 
      </table> 
<%}}%> 

但我不確定kati[]數組的索引。你可以發佈數據庫原始字符串示例(分割之前)。 另外(如果您仍然使用此版本)最好檢查kati[]陣列中拆分line後有多少個元素。有沒有十個元素?

+0

this是來自db 12#0#100000.0#house#100.0#1#autonomous#100#100#100 $ 12#0#100000.0#house#100.0#1#autonomous#100#100#100 – gimbo 2014-09-03 14:11:39

+0

的原始字符串似乎很好的工作謝謝 – gimbo 2014-09-03 14:15:54

+0

在我的例子中,似乎像'kati []'中的索引是正確的。 – gooamoko 2014-09-03 14:18:21

0

如果您收到特殊字符kati數組,那麼你可以使用標籤庫,並使用

c:out作爲

<c:out value=${yourArrayvalue} escapeXml='true'/> 

所以你會得到所需的輸出

+0

kati只包含沒有特殊字符的字符串 – gimbo 2014-09-03 13:59:56