java - 通过 LinkedList 在 JSP 中打印名称而不是按顺序打印
问题描述
我正在尝试将排序顺序应用于 HTML 搜索表单,并按指定顺序打印结果。我一生都无法弄清楚为什么它不起作用,除非我不知道关于 LinkedLists 的一些东西。我对开发比较陌生,所以这是可能的。请记住,这是为我的编码训练营分配的任务,所以虽然我感谢所有建议,但我们被要求不要导入除提供的库之外的任何库,并且我们也被要求不要使用 servlet。下面的代码
这个块可以在我的 JDBC 中找到。
public List<Customer> searchAndSortCustomers(String search, String sort) {
List<Customer> customers = new LinkedList<>();
String query = "SELECT first_name, last_name, email, activebool FROM customer WHERE first_name ILIKE ? OR last_name ILIKE ? ORDER BY ?";
SqlRowSet result = jdbcTemplate.queryForRowSet(query, "%" + search + "%", "%" + search + "%", sort);
while(result.next()) {
customers.add(mapRowToCustomer(result));
}
return customers;
}
辅助方法 mapRowToCustomer
private Customer mapRowToCustomer(SqlRowSet result) {
Customer customer = new Customer();
customer.setFirstName(result.getString("first_name"));
customer.setLastName(result.getString("last_name"));
customer.setEmail(result.getString("email"));
customer.setActive(result.getBoolean("activebool"));
return customer;
}
带有表单等的 JSP 页面
<form method="GET" action="customerSearch">
<label for="search">First or Last Name</label>
<input name="search" type="text"/>
<br>
<br>
<label for="sort">Sort By</label>
<select name="sort">
<option value="first_name">First Name</option>
<option value="last_name">Last Name</option>
<option value="activebool">Active</option>
</select>
<br>
<br>
<input type="submit"/>
</form>
<c:if test="${not empty param.search}">
<table id="customer-flex">
<tr>
<th>Name</th>
<th>Email</th>
<th>Active</th>
</tr>
<c:forEach var="customer" items="${customers}">
<tr>
<td>${customer.firstName} ${customer.lastName}</td>
<td>${customer.email}</td>
<td>${customer.active}</td>
</tr>
</c:forEach>
</table>
</c:if>
该查询在带有复制/粘贴值的 dbvisualizer 中工作,只是为了仔细检查我没有为 db 列标题犯任何拼写错误。任何建议将不胜感激!
谢谢,
解决方案
“您不能将要排序的列作为?参数传递给 ORDER BY 子句。– a_horse_with_no_name”
这是正确答案,再次感谢!
推荐阅读
- bash - insert hosts in check via for loop
- php - Not getting id from URL in Laravel 6
- android - 了解 Kotlin 中的顶级 get()
- node.js - 嘿,伙计们,当我尝试运行以下命令时,出现错误“只能使用 'esModuleInterop' 标志默认导入”
- c# - Unity 尝试使用 Color.RBGtoHSV 从获取像素中获取色调,但 H 始终返回 0
- java - IntelliJ VCS ui 问题我该如何解决?
- git - 后面提交的 MR 很危险?
- assembly - DOS 图形模式下的程序集设置字体背景颜色
- r - 如何基于 R 中另一个数据帧的数学运算创建数据帧
- tsx - React,TSX 解析错误:预期表达式