问题描述
我有一个ArrayList,其中包含称为“历史记录”的定制对象。当我单击链接时,在代码示例顶部的一个名为“ Deposit1”的链接,将以正确的顺序显示列表。
但是,我希望能够在用户单击表中的链接“ Deposit2”时反转列表,该链接仅在用户单击第一个链接“ Deposit1”之后显示。
现在什么也没发生,但是我可以在点击第二个链接时成功system.out.print。
反正有这样做吗?
请注意,已对代码示例进行了修剪,以使概述更好。
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>
解决方法
您可以在href标记中同时为2
和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
,
将第一个if-sortDep1
更改为:
if (sortDep1 != null || sortDep2 != null) {
然后,当其中一个有效时,总是输入部分代码。