开发者

JSTL : Find the total of size of two lists

开发者 https://www.devze.com 2023-04-08 07:45 出处:网络
I have two lists on a page and showing combined size of these two lists. Here is my code 开发者_如何转开发<c:set var=\"totalAvailableVehicles\" value=\"${fn:length(searchResult.availableVehicleLi

I have two lists on a page and showing combined size of these two lists.

Here is my code

开发者_如何转开发<c:set var="totalAvailableVehicles" value="${fn:length(searchResult.availableVehicleList)}"/>
<c:set var="totalUvailableVehicles" value="${fn:length(searchResult.unavailableVehicleList)}"/>
<c:out value="${totalAvailableVehicles + totalUvailableVehicles}"/></strong> record found matching your search criteria</p>

Is there any better way to achieve same without writing custom tag/functions?


I'm not sure what you mean with a "better way". This looks perfectly fine. You could also do it without <c:set>:

<strong><c:out value="${fn:length(searchResult.availableVehicleList) + fn:length(searchResult.unavailableVehicleList)}"/></strong> record found matching your search criteria</p>

However, whether that's better readable/maintainable is questionable.

You could also move that to a getter method of the SearchResult bean:

public int getTotalResultSize() {
    return availableVehicleList.size() + unavailableVehicleList.size();
}

with

<strong>${searchResult.totalResultSize}</strong> record found matching your search criteria</p>

Note that the <c:out> is not necessary here (it'll work as good in JSP 2.0 and newer). The benefit of <c:out> is the HTML-escaping of user-controlled input in order to prevent XSS attacks, but since it concerns here non-user-controlled input of type int, there is really no XSS attack risk.

After all, it really doesn't matter as long as you end up with a degree of readability/maintainability which your team agrees in.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号