Velocity模板动态表格生成异常求助:数据全部显示在一行
Hey Rahul, let's get that dynamic table working properly! The root issue here is that all your table cells are being rendered inside a single <tr> (table row), which is why every loan's data ends up crammed into one line. Let's fix your Velocity template to create a new row for each loan entry.
First, let's confirm your Java code is setting up the data correctly—your sample output shows Map0 and Map1 each contain a full loan record, which is exactly what we need. The problem is entirely in how you're looping through this data in the template.
Here's the corrected template section for your table:
<table style="width:100%; border: 1px solid #dddddd; font-family: Times New Roman, Times, sans-serif;"> <tr> <th>Loan Type</th> <th>Loan Amount</th> <th>Interest Rate</th> <th>Loan Repayment Date</th> <th>Tenor</th> <th>Proposed Limit</th> <th>Approved Limit</th> </tr> <!-- Loop through each loan entry in your main Map --> #foreach ($mapEntry in $Map.entrySet()) #set ($loanRecord = $mapEntry.getValue()) <!-- Store the sub-map with loan details --> <tr> <td>$loanRecord.get('loanType')</td> <td>$loanRecord.get('loanAmount')</td> <td>$loanRecord.get('interestRate')</td> <td>$loanRecord.get('loanRepaymentDate')</td> <td>$loanRecord.get('tenor')</td> <td>$loanRecord.get('proposedLimit')</td> <td>$loanRecord.get('approvedLimit')</td> </tr> #end </table>
Key changes made:
- Moved the
<tr>inside the#foreachloop: Now every loan entry (likeMap0andMap1) will generate its own table row, instead of all cells going into one row. - Directly accessed known keys: Since you already know the exact field names in each loan sub-map, we don't need to loop through every entry in the sub-map and check keys. This makes the code cleaner and faster.
- Added a readable variable:
$loanRecordstores the sub-map for each loan, making the template easier to read and maintain.
If you want to keep using your string variables:
If you still prefer to use the $str1 to $str7 variables you defined, here's an adjusted version that works with them:
<table style="width:100%; border: 1px solid #dddddd; font-family: Times New Roman, Times, sans-serif;"> <tr> <th>Loan Type</th> <th>Loan Amount</th> <th>Interest Rate</th> <th>Loan Repayment Date</th> <th>Tenor</th> <th>Proposed Limit</th> <th>Approved Limit</th> </tr> #set($str1="loanType") #set($str2="loanAmount") #set($str3="interestRate") #set($str4="loanRepaymentDate") #set($str5="tenor") #set($str6="proposedLimit") #set($str7="approvedLimit") #foreach ($mapEntry in $Map.entrySet()) #set ($loanRecord = $mapEntry.getValue()) <tr> <td>$loanRecord.get($str1)</td> <td>$loanRecord.get($str2)</td> <td>$loanRecord.get($str3)</td> <td>$loanRecord.get($str4)</td> <td>$loanRecord.get($str5)</td> <td>$loanRecord.get($str6)</td> <td>$loanRecord.get($str7)</td> </tr> #end </table>
Your Java code doesn't need any changes—it's already correctly structuring the data into a map of loan records. Just update your template with either of these versions, and you'll get a proper 2-row table (one for each loan entry) as expected.
内容的提问来源于stack exchange,提问作者Rahul

