I have an user interface that print user, and I wan't to have a filter by country.
I made a classic <select />
element.
In JSP I have
<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
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".
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".
At the end, the DAO object will make a query like
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 ?