Jack Parker
Jack Parker

Reputation: 569

Using A DropDown List Selection in an If Statement

OK, I've created a DropDownList associated with a search field. What I want to do, is have the selection in the dropdown cause a method to trigger based on the selection, and to search on that. I thought I would do something like if List.contains, and base it on that, but it doesn't appear to be working.

I have an arraylist setup to populate the dropdown, and the beginnings of an if statement in my controller.

List<String []> propertyList = new ArrayList<String []>();

         propertyList .add(new String[]{"EmailAddress","Email"});

         propertyList .add(new String[]{"FirstName","First Name"});

         propertyList .add(new String[]{"LastName","Last Name"});

    mav.addObject("propertyList ", propertyList 

   if (propertyList .contains("LastName")) {

           \\Code that needs to fire

 }

Any suggestions would be appreciated.

Upvotes: 0

Views: 1724

Answers (1)

Neeraj Jain
Neeraj Jain

Reputation: 7730

Your propertyList is a list of Objects (i.e Array of String) , So

propertyList .contains("LastName") 

will never satisfy the condition , You need to make List<String> Instead of List<String []>

or you can use Map<String,String> if you want a Key value pair

Map<String,String> propertyList = new HashMap<String,String>();

         propertyList.put("EmailAddress","Email");

         propertyList.put("FirstName","First Name");

         propertyList.put("LastName","Last Name");

   mav.addObject("propertyList ", propertyList);

   if (propertyList.containsKey("LastName")) { // Here you can check the key

           \\Code that needs to fire

 }

Update

How to iterate Map in jsp


<c:forEach var="entry" items="${propertyList}">
      <c:out value="${entry.key}"/>
      <c:out value="${entry.value}"/> 
</c:forEach>

Upvotes: 2

Related Questions