2

I have a class language having features ID and Name. I have another class Language_List that has ArrayList<language> lang as a member variable.

In my JSP page I want to access the Name variable of ArrayList<language> lang using EL and For each Loop.

<c:forEach var="language" items="${languages.lang}">
  ${language}<br>
</c:forEach>

However, it doesn't show ant result and intellisense doesn't work too. Anyone who can help me with this PS: languages is a Bean variable contain list of languages from DB I tried this and got this

 <b>${languages.lang}</b>
HTML

[sakila.language@f1541c, sakila.language@63c8fabf, sakila.language@1fc644c7, sakila.language@11cd751d, sakila.language@47c3cc0c, sakila.language@7894ca3, sakila.language@47066532, sakila.language@74ddda0b, sakila.language@1116441e, sakila.language@4cd21655, sakila.language@74b84dd9, sakila.language@6fff1d6c, sakila.language@55e4d6e5, sakila.language@22d88071, sakila.language@33d88c96, sakila.language@4df5e671, sakila.language@4aec2cb3, sakila.language@576ac232, sakila.language@76a6dbd7, sakila.language@44ab3d1c, sakila.language@46391c7c, sakila.language@4f7d34e8, sakila.language@251c941d, sakila.language@77400ef3]

3
  • In your sample, language is a variable, not a class. Why not making the base class implement Iterable and provide a correct .toString() implementation for elements? Commented Dec 23, 2012 at 14:30
  • @fge what should I do in the body of .tostring(),You want to say that you can't access an Arraylist using EL? Commented Dec 23, 2012 at 14:38
  • I don't say you cannot access it, I just say that if the array is the only iterable thing in your class, you may as well make the class implement iterable of the elements in the array. As to .toString(), it depends on the information you want to be displayed. Commented Dec 23, 2012 at 14:42

1 Answer 1

2

The EL doesn't access fields of your objects. It accesses bean properties of your objects. This means that ${languages.lang} is translated to a call to languages.getLang().

If you don't have such a getter, you'll get an exception though. If it just doesn't display anything, it's probably because languages is null, or because its lang list is null or empty. To confirm or infirm those guesses, we need to see the code where you create and populate the bean and its list of languages, and where you store it somewhere to make it accessible from the JSP.

Another possibility is that you forgot to declare the core taglib at the beginning of the JSP. To confirm or infirm that, paste the code of the JSP, and the HTML code generated by the JSP (using View page source in the browser)

Sign up to request clarification or add additional context in comments.

2 Comments

i have made an Edit, and the lang variable is not null as I did the debugging.If I use scriplets <% %> I can traverse easily..
So, it's very far from printing nothing. If you want it to display something meaningful, then display internal properties of each object, or override the toString() method in the Language class. (Please rename language to Language. Classes always start with an upper-case letter by convention in Java).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.