Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting a List in the request or session scope using JSTL

 
Hemant Kawale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you set an arraylist or an array to the session or request, via JSTL in a JSP page.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Assuming that xyz is a scoped variable that is an array or List (or really any other type of object):



or



Or are you really asking if you can create and populate such elements from within the JSP page? In which case, I'd ask why you'd do this in a JSP rather than in the servlet controller.
[ February 07, 2006: Message edited by: Bear Bibeault ]
 
Hemant Kawale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hemant Kawale:


or



Or are you really asking if you can create and populate such elements from within the JSP page? In which case, I'd ask why you'd do this in a JSP rather than in the servlet controller.

[ February 07, 2006: Message edited by: Bear Bibeault ]<hr></blockquote>[/QB]





I have to convert some pages jsp pages that have declared and populated a arraylist in scriptlets.
I have to remove these scriptlets and use Jstl instead.

So is there any way of replacing this :
<%
ArrayList A = new ArrayList();

A.add("Error Message:");
A.add("Session Not Obtained");
A.add("Solution");
request.setAttribute("e",A);
%>
with JSTl tags
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best approach would be to factor that code out into the controller and establish them in the appropriate contexts before forwarding to the JSP.
 
Hemant Kawale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot change anything else as this would affect the action class, that is taking this out of the request as an arraylist.



So is there any direct replacement for the mentioned code....???

Like <c:set var=A[0] value="something />
<c:set var=A[1] value="something />

and the setting the <c:set var=${A} scope="request />
???
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you move it into the action? It's really the right thing to do.

I've never tried to create and populate a List on a JSP page. It's quite possible to do for JavaBeans and even Maps with a combination of <jsp:useBean> and <c:set>.

But Lists... not so sure...

If it's something you have to do on the page (I would not), perhaps you can create a List or array in a JavaBean wrapper so that you can manipulate it via the EL and JSTL, or you could just resort to some custom actions to do it.

But really, factoring it into the controller/action is the best practice...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot change anything else as this would affect the action class, that is taking this out of the request as an arraylist.


As Bear said, you should do this kind of stuff outside JSP. There must be a way to set this in the controller forwarding to this page.

I don't think you can do it using JSTL.
 
Hemant Kawale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i guess there is no way of setting a list into the session or request scope via JSTL.

Ok. thanks a lot for the help guys.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can set anything you want into a scoped variable. The issue is the code necessary to create and populate such constructs.

At this point I'd be asking what you are trying to accomplish by refactoring the pages away from scriplets. The whole purpose of using the JSTL and EL on scriptless pages is to refactor the pages to pure view elements.

If you are unwilling to move inappropriate processing off the page and are just going through the motions of converting scriplet processing to JSTL equivalents (not always possible, as you have seen), what's the point of the exercise? What are you trying to gain by the conversion?
 
Hemant Kawale
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its just that somebody wrote a bad piece of code and now i have to deal with converting it into JSTL.

Cant affored to refactor it..cus thaen it would affect the entire application.

I know its bad programming . . .
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not replace a smelly piece of code by another one
Doing this with JTSL won't make it less smelly.

I hate this kind of job
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you've been given a job to do without the tools or responsibility to do it right. That's a tough place to be.

Well, as I said before, you could punt back to beans or custom actions if need be.
 
Stefan Evans
Bartender
Pie
Posts: 1671
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed with the general consensus: you can't populate an array/list using JSTL.
The best place to do it would be an action.

If the code HAS to be on the page you can
- leave it as scriptlet code
- write a custom tag to handle it

I'd see the custom tag working something like this:

 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic