【发布时间】:2010-06-29 13:32:06
【问题描述】:
我有一个打印用户的用户界面,但我不想按国家/地区进行过滤。
我做了一个经典的<select />元素。
在 JSP 中我有
<select id="country" onchange="return filter();">
<option value="">...</option>
<c:forEach var="country" items="${countries}">
<option value="${country.id}"
${country.name}
</option>
</c:forEach>
</select>
问题是一些用户没有国家,所以我需要处理 2 个过滤器: - 打印所有用户,无过滤器 - 只打印没有国家/地区的用户
所以我想知道对 Java 说的最佳方式是什么:“找到我所有的用户”和“找到我所有没有国家/地区的用户”。
我有一些想法:如果 countryId = 0,服务器将其翻译为“没有国家/地区的用户”,如果 countryId = null,则服务器将其翻译为“所有用户”。
最后,DAO 对象会进行如下查询
public List<User> findByCountry(Integer countryId){
query = "select * from users"
if(countryId==0){
query+= " where country_id is null"
}
else if(countryId==null){
query += " where country_id = " + countryId;
}
return query results...
}
这样可以吗,或者这很难看,还是有人有更好的模式来做到这一点?
【问题讨论】:
标签: java model-view-controller design-patterns anti-patterns