2014-09-03 48 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