0

I need to loop over a particular list of rows pertaining to a database column, which is of type Integer. This column in the database is the foreign key to the parent column ID in the same table.

I have to match the both these values and print in the JSP if there is a match.

I've gone through this link Evaluate list.contains string in JSTL and have been trying over to get it working. I don't know where exactly I've gone wrong.

Here is what I have so far:

<c:forEach var="List" items="${Section.LangList}">
    <c:if test="${List['Lang'] == List['Id']}">
        ...
    </c:if> 
</c:forEach>    

This is one of the things that I've used to compare the values. I've tried the contains and the <c:set> too. Is there something that I'm missing?

Community
  • 1
  • 1
Nani
  • 214
  • 4
  • 13
  • *Is there something that I'm missing*: to include the code of the class of the objects contained in the list. – JB Nizet Jan 13 '12 at 14:40

2 Answers2

2

You need to start property names with lowercase. The ${Section.LangList} won't work, it has to be ${Sections.langList}.

<c:forEach var="List" items="${Section.langList}">
    <c:if test="${List['Lang'] == List['Id']}">
        ...
    </c:if> 
</c:forEach> 

If that still doesn't work, another possible cause is that the data type of the both sides is different, even though they contain the same value. E.g. String value of "42" versus Integer value of 42. You need to make sure that the data types are the same.


Unrelated to the concrete problem, your naming convention is terrible. Each item of a List<Language> surely isn't another List, but likely the Language class. Also, using the brace notation to access a property with a fixed name is unnecessary. Just use the ${bean.property} notation. The following will make your code more conform naming conventions and more self-documenting:

<c:forEach var="language" items="${section.languages}">
    <c:if test="${language.lang == language.id}">
        ...
    </c:if> 
</c:forEach> 

I only still wonder how ${language.lang} makes sense in this context.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

try something like this:

<c:forEach var="item" items="${Section.LangList}">                   
    <c:if test="${item.lang eq item.id}">
        .....
    </c:if>
</c:forEach>    

if it wont work show us you class that is on iterated list

dantuch
  • 9,123
  • 6
  • 45
  • 68