简体   繁体   English

如何处理空值 <select> HTML过滤器?

[英]How to deal with null value in <select> HTML filter?

I have an user interface that print user, and I wan't to have a filter by country. 我有一个可以打印用户的用户界面,并且我不想按国家/地区进行过滤。 I made a classic <select /> element. 我做了一个经典的<select />元素。

In JSP I have 在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>

The thing is some users doesn't have a country, so I need to deal with the 2 filters : - one that print all the user, no filter - one that print only users that doesn't have a country 问题是某些用户没有国家/地区,因此我需要处理2个过滤器:-一个过滤器显示所有用户,不过滤器-一个过滤器仅显示没有国家/地区的用户

So I'm wondering what is the best way to say to Java : "find me all users", and "find me all users who doesn't have a country". 因此,我想知道对Java说最好的方法是:“找到所有用户”,然后“找到所有没有国家的用户”。

I have some idea : if the countryId = 0, the server translate it to "users who doesn't have a country, and if countryId = null, the server translate it to "all users". 我有一个想法:如果countryId = 0,则服务器将其转换为“没有国家的用户,如果countryId = null,则服务器将其转换为”所有用户”。

At the end, the DAO object will make a query like 最后,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...
}

So is this ok, or is this ugly, or someone have a better pattern to do this ? 那可以吗,还是丑陋,或者有人有更好的模式来做到这一点?

I would actually come up with two DAO APIs: 我实际上会提出两个DAO API:

public ... findAllUsers(...) {...}

public ... findAllUsersWithoutACountry(...) {...} 

The problem with your approach, in my honest opinion, is your API is not explicit due to the dynamic SQL code. 坦白地说,您的方法存在的问题是由于动态SQL代码,您的API并不明确。 It makes it harder for your coworker to understand your code. 这使您的同事更难理解您的代码。 Second, these are 2 different tasks, while they are similar, it is best to come up with two explicit methods. 其次,这些是2个不同的任务,尽管它们相似,但最好提出两种显式方法。 This is much easier to unit test and you have less cyclomatic complexity because you have less control flow in the method. 这使单元测试更加容易,并且由于简化了方法中的控制流程,因此循环复杂度也降低了。 Further, the code is easier to understand because other developers don't need to wonder why you are testing the countryId against 0 or null which doesn't convey whole lot of meaningful message to them, other than it is just a quick hack to solve your current problem. 此外,该代码更易于理解,因为其他开发人员无需怀疑您为什么要针对0或null测试countryId,这并不能向他们传达大量有意义的消息,而不仅仅是解决问题您当前的问题。 3 weeks down the road, and you will be wondering why you are testing against these weird values yourself. 在接下来的3周内,您会想知道为什么要自己针对这些怪异的价值观进行测试。 :) :)

I think your approach is correct, but you might want to make any user WHERE CountryID=0 || CountryID IS NULL 我认为您的方法是正确的,但您可能希望使任何用户的WHERE CountryID=0 || CountryID IS NULL WHERE CountryID=0 || CountryID IS NULL use the 'All Users' label, this way you can filter to see anyone without a country set. WHERE CountryID=0 || CountryID IS NULL使用“所有用户”标签,这样您可以过滤以查看未设置国家/地区的任何人。 Then you can fix those users if desired/needed. 然后,您可以根据需要修复这些用户。 But I think overall your solution is good. 但我认为您的整体解决方案很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM