• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL type casting  RSS feed

 
Krupa Eng
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a arraylist of myobjects.

How can do the following with JSTL.

for(int i=0;i<myObjList.size();i++){
myObjItem curritem = (myObjItem ) myObjList.get(i);
}

The problem I am having is casting to myObjItem type. How do I do that in JSTL

Thanks,
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSTL handles the casting for you.
Here is an example that iterates over an arrayList of UserBean objects:


The array list was bound to request scope in a servlet.
It's id is "activeUsers".
 
Krupa Eng
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response.


I have a mainobject that has myObjectsList (an arrayList of myObjcts) and a simple arrayList of Strings as a properties. So I have my mainobject in the request scope.

So I had to do the following to loop thru the 2 lists.
ArrayList objList= myObjectslist.getObjList();
ArrayList simpleList = myObjectsList.getSimpleList();

How do I cast this Objects in the objList.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you feel the need to cast?
 
Krupa Eng
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a mainobject that has myObjectsList (an arrayList of myObjcts) and a simple arrayList of Strings as a properties. So I have my mainobject in the request scope.

So I had to do the following to loop thru the 2 lists.

ArrayList objList= myObjectslist.getObjList();
ArrayList simpleList = myObjectsList.getSimpleList();
pageContext.setAttribute("objList",objList);

When do the following,

<c:forEach var="item" items="${objList}" >
<c ut value="${item}" />
</c:forEach>

It is outputting as
mypackage.myObjItem@45e66456
not the value.

Can any one please help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value you are expecting it to be? How is it stored in that class? Have you supplied an override for the toString() method on that class?
 
Jorge Baroudi
Greenhorn
Posts: 10
jQuery Ruby Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have the same problem, i need to cast to the specific entity type to retrieve several (bean) properties, and when i try to do it directly using EL (for example ${item.name}, item is from var="item" of a <c:forEach ..> tag) there is a javax.servlet.jsp.el.ELException: Unable to find a value for "name" in object of class "java.lang.String" using operator ".", but i'm storing the objects like this:

In my class method i do something like this:

<!-- Java Code -->
// initialization of the item
MyItem item = null;

// rs is of type ResultSet previously populated by a query
while( rs.next )
{
item = new MyItem( );
item.setName( rs.getString("name") );
// an array of type ArrayList
array.add( item );
}

<!-- End of Java Code -->

I run a test within main method of the class method (called getList()) and it all worked fine...

I retrieve that list in a jspf file (i don't think there's a problem there), didn't use a servlet as a controller because i had to add the call in every controller so i decided to put the code in the view directly (something just to be practical, despite breaking a little MVC rule ).

And well, what else, i overrided the toString method but the first value appears with a [... and the last value with a ..] so i really thought of the casting as a solution.

May i have a hint from you guys of where should i look for the values of MyItems objects ?

Thousand thanks for your atention.

George Baroudi.
[ January 30, 2006: Message edited by: Jorge Baroudi ]
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to spell things out.
You don't actually need to cast anything in EL. EL doesn't really even have the concept of "type"
EL is not a strongly typed language like java is.
It uses reflection at runtime. As long as the object is of they type you are expecting, you can use EL on it.

>Unable to find a value for "name" in object of class "java.lang.String"
This error message commonly occurs when you miss out the ${expr} markers on the items attribute of a forEach loop.
You haven't posted any JSP/JSTL code here, so I am just guessing, but here is what I think it should be:



[ January 30, 2006: Message edited by: Stefan Evans ]
 
Jorge Baroudi
Greenhorn
Posts: 10
jQuery Ruby Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Stefan, i'm going to put some code to be more specific:



I appreciate any help on this issue

George.
[ January 30, 2006: Message edited by: Jorge Baroudi ]
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<c:set var="arrayNewspapers" scope="page" >
<%=newsRules.getList(null)%>
</c:set>

The <%= expr %> tag in template text where it is evaluates to a string.
The c:set tag is saving the output of the toString() method.

You might have better luck with it like this:
<c:set var="arrayNewspapers" scope="page" value="<%= newsRules.getList(null) %>"/>

Even better, eliminate the need for it.
As I am sure you are aware, EL cannot call a method that takes parameters. So provide a method that doesn't take parameters (you're not using it anyway)
Add the following method to the NewspaperRules class:


This will then let you get the list via an EL expression such as ${newsRules.list}.
 
Jorge Baroudi
Greenhorn
Posts: 10
jQuery Ruby Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i was thinking in a redirection like you said (public ArrayList getList(){return this.getList(null);} ) to try the ${newsRules.list} effect, and i'll put the value within the c:set tag to see if it works better.

i'll be back to you in, say 10 minutes

Thanks for your reply Stefan
 
Jorge Baroudi
Greenhorn
Posts: 10
jQuery Ruby Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gracias Stefan!!!

It worked perfectly!

What was happening? what was i storing in the arrayNewspapers ArrayList variable? i checked the tomcat generated servlet for the jsp file and... well, i did a lot of things and it resulted to be the easiest one

i tried to retrieve within a scriptlet the pageContext to cast the news variable (from the forEach tag) but nothing worked... oh well... i'm happy now, and my code looks "scriptlessly" fine thanks to you Stefan.

PS. Gracias!!! is Spanish for Thank you!!!

Now i have to face a much serious problem... paging with SQL server... now that is a problem! hehehehe.

Thousand thanks!

${mytags: DisplayJumpingHappyBeanGeorge()}
^----- Note: That is definitively not the method name... check the DD file
^----- The ${mytags: Dis... space is left intentionally

Regards.
[ January 30, 2006: Message edited by: Jorge Baroudi ]
 
Steven Lawrence Hunter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this thread when encountering a similar problem, and I thought I should share what I found out. If you screw up your c:forEach, and include a trailing space in the items attribute, each item will be turned into a string. For example:

<c:forEach items="${laborGtnList} " var="laborGtnItem" varStatus="count">

note the trailing space after ...List} ". Now when you reference an item from the list, like:

<td>
${ laborGtnItem.type }
</td>

you'll get the ELException: Unable to find a value for "type" in object of class "java.lang.String" using operator "."

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!