链接上的反向数组列表

lxkprmvk  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(291)

我有一个arraylist,它由名为“历史”的定制对象组成。当我单击一个链接(代码示例顶部名为“deposit1”)时,我会按正确的顺序显示列表。
但是,我希望在用户单击表中的链接“deposit2”时能够反转列表,该链接仅在用户单击第一个链接“deposit1”后显示。
现在什么都没有发生,但我可以系统。输出。打印成功的secound链接点击。
有什么办法吗?
请注意,为了更好地概述,对代码示例进行了裁剪。

ArrayList <history> history = historyDAO.getHistory(x, y);

<li>
     <a href="history.jsp?Dep1=valueOfDep">Deposit1</a>
</li>

String sortDep1 = request.getParameter("Dep1"); 
String sortDep2 = request.getParameter("Dep2");

if (sortDep1 != null) {         

    <table class="table text">
      <tr>
        <th>Result</th>
        <th>Sport</th>
        <th>Deposit2<a href="history.jsp?Dep2=valueOfDep">Deposit2</a></th>
      </tr>

 if (sortDep2 != null) {
    Collections.reverse(history);
   }   

for (int i = 0; i < history.size() && i < list1.size(); i++) {

    <tr>
            <td><%=history.get(i).getRes()%></td>
            <td><%=history.get(i).getSport() %></td>
            <td><%=history.get(i).getDeposit()%></td>
    </tr>

<%}}%>

</tr>
</table>
pkmbmrz7

pkmbmrz71#

更改第一个 if-sortDep1 收件人:

if (sortDep1 != null || sortDep2 != null) {

当其中一个有效时,总是输入代码的这一部分。

3wabscal

3wabscal2#

你可以通过 2 href标记中的参数 Dept1 & Dept2 以便 sortDep1 不为空,它将进入内部 if-statement 根据url中的值,可以反转列表。即:

if (sortDep1 != null) {         
<table class="table text">
<tr>
   <th>Result</th>
   <th>Sport</th>
   //pass 2 parameter in url for dep1 & dep2
   <th>Deposit2<a href="history.jsp?Dep1=valueOfDep&Dep2=reverse">Deposit2</a></th>
</tr>
//check if not null and value is reverse
  if ((sortDep2 != null) && (sortDep2.equals("reverse")) {
    Collections.reverse(history);
    }   
 //other codes

相关问题