Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean - doubt in question

 
Adam Mazurek
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Can anyone tell me, why this code:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<jsp:useBean id="users" class="java.util.Vector" />
<html>
<body>
<%
users.add("Paul");
users.add("Kathy");
users.add("Frank");
%>
<c:forEach var="name" items ="${users}" varStatus="counter">
<c ut value="${counter.count}"/>
<c ut value="${name}"/>
</c:forEach>
</body>
</html>


produces output:
1 Paul 2 Kathy 3 Frank

I thought that useBean creates bean in page scope and we don't have access to it in scriplets.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thought that useBean creates bean in page scope

If you don't specify another scope, that's true. But even in that case, can you imagine where the bean will be declared when your JSP is translated into Java ? It will be declared locally, so any scriplet after the useBean tag can use it. Guess what will happen in the following case :
 
Charmy Madhvani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you achieve this code without using scriplet?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don't think that you can. Remember that EL is not made to set values, but to get values.
And please do not hijack other ranchers' thread If you have a question not related to the original question, please make your own thread. Thank you.
 
Charmy Madhvani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll take care next time. Thanks anyways
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys

Can I use the core tag c:forEach for iterate over all teh attributes from a bean ? As far as I know I can only iterate over the element from a Collection,Map,array or comma-delimited Strings.


Regards,
M
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mihai,
the "do not hijack other ranchers' thread" policy applies to everybody Please make a new thread when you have an unrelated question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic