Sthita
Sthita

Reputation: 1782

How to prevent duplication in dropdownlist using JSTL

How to prevent duplication in dropdownlist using JSTL .

<select class="abc" name="folder" >
<c:forEach items="${model.abc}" var="folder" varStatus="status">
<option value="${folder}">${folder}</option>
</c:forEach>
</select>

Suppose my model object abc is having some data like :

Folder :"abc" , "bcd", "abc"

How to prevent abc to be populated in dropdown multiple times?

Upvotes: 0

Views: 1615

Answers (3)

gowtham
gowtham

Reputation: 987

Better to remove duplicates in Java code and return a unique list to the Jsp.

If your object abc is an ArrayList, Convert it to Set and again back to List.
As we all know Set doesn't allow duplicates, All the duplicates in list will be removed.

Sample code:

    List<String> abc=new ArrayList<String>();
    abc.add("abc");
    abc.add("def");
    abc.add("abc");

    abc = new ArrayList<String>(new HashSet<String>(abc));

Result: No duplicates in abc.

Upvotes: 2

Saif Asif
Saif Asif

Reputation: 5658

Its never a good idea to put (even the tiniest) business logic inside your view. The responsibility of the view part of MVC is to just , well, generate the view for the end user ! No processing , no business logics involved.

So I suggest you handle duplication on the business logic end and then send the duplicate-free list over the view and display it

Upvotes: 2

Alex
Alex

Reputation: 11579

Prepare your list in backend (without duplication) and display it.

Upvotes: 2

Related Questions