wood burning stoves 2.0*
The moose likes Struts and the fly likes Arraylist and Hashtable issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Arraylist and Hashtable issue" Watch "Arraylist and Hashtable issue" New topic
Author

Arraylist and Hashtable issue

vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Hi Ranchers,

I am having an ArrayList with Hashtables. I iterated ArrayList on jsp. But iam not getting values in select box..please check my jsp, dao and dbutils.

MyJSP



MY DAO





Please check my code and tell me where iam doing mistake.

Thanks
Vipul Kumar.


Thanks
Vipul Kumar
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Before you look at JSP, make sure your result set has records then array list is not empty.

I suggest you may want to use the old fashion simpler way (aka looping the result set directly) inside your listUsers() method to make sure it's not the query. If that works, then it's your DBUtil.rsToArrayList() method.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Hi Rancher,

Thanks for the reply. Data is coming from database. Please verify my jsp. tell me how to access hashmap value in a arraylist

Thanks
vipul kumar
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

requirement is you need to show the list of map key/value's right?

I am going to tell you how to display list of maps in jsp.

here you go: Use Servlet and JSP. just call the servlet first, then forward to jsp from there...

in servlet:


In JSP:(index.jsp)


simple! is not it?

* if you follow design1 architecture(using only JSPs) . then you need to move servlet logic to a class
and the class processing should take before the JSTL loop.

vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Hi Rancher,

Thanks for response. which version of core tags allowing expressions in items attribute.?

Thanks
Vipul Kumar.
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Hi Rancher,


i am getting this exception .

Exception:- According to TLD or attribute directive in tag file, attribute items does not accept any expressions

that is why, i asked jstl core tags version

Thanks
Vipul Kumar.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

vipul bondugula wrote:
Exception:- According to TLD or attribute directive in tag file, attribute items does not accept any expressions

do you use <%= %> ? perhaps, it is not supported in JSTL1.0
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Hi Rancher,


I am using EL Expressions.

Thanks
Vipul Kumar
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
You are using JSTL1.0. You should be using JSTL1.1 at least.

The issue is with your tag library declaration:

should be

(note the subtle addition of /jsp in there)

JSTL1.0 is intended for old containers - Servlet 2.3/JSP1.2. (eg Tomcat 4)
If you are using anything newer than that then use the JSTL1.1 uri.
JSP2.0 is the point at which EL expressions started being interpreted by the server, which was a fundamental change for the tag library - so they gave it a new URI to distinguish which to use.




vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Thanks Stefan,


Expression Language issues solved. EL expressions are embedded in jsp 2.0. Thanks for the information.

But my issue is , start of the post. to display elements in hashmap which is in arraylist..

Thanks
Vipul Kumar.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Sorry, I thought Seetharaman had already answered your query, and my answer was to help with troubleshooting an error message with his solution.
Taking a closer look at his answer, it doesn't seem to be what you were implementing.

This should be closer:



Changes I made
- added EL expression markers to the "items" attribute on your c:forEach.
- changed your <c:forEach> loop to nest the <option> values within it.
- removed the useBean statements. They are not necessary when using EL and JSTL. Only when you are using scriptlets as they define a scriptlet variable on the page as well as putting the bean in scope.
- changed the syntax ${mapItem["user_id"] } to ${mapItem.user_id} which is equivalent.

That should do it.

Returning a List of Maps indexed by column name is a bit of a code smell because database columns might be renamed etc without you ever being informed about it, thus breaking the page.
Preferable would be to define a java bean to return the values to the page that you want, and load values from the result set into a List of Beans.

vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Thanks Stefan

Returning a List of Maps indexed by column name is a bit of a code smell because database columns might be renamed etc without you ever being informed about it, thus breaking the page.
Preferable would be to define a java bean to return the values to the page that you want, and load values from the result set into a List of Beans.


Even though below question is irrelavent , please answer query....

Suppose if iam using struts 1.2. can i use form bean as a java bean to return values to the page...form bean is also a java bean , right?

Thanks
Vipul Kumar.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
If you are using Struts1.2 you might have more luck in our Struts forum :-)

But yes, you can use the form bean to return values to the page. In fact I'm pretty sure that is a common use case.
Normally you should work within the bounds of the framework though rather than short-circuiting things.

However, with my previous answer returning a list of beans rather than a list of maps, my answer remains the same.
A List of beans is always preferable to a list of maps because it defines more strictly what the values are, and they won't change if you change your database columns.
The only difference with stripes is I might use the form to return that list rather than setting it as an scoped attribute.



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Copied to the Struts forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arraylist and Hashtable issue
 
Similar Threads
problem in running a servlet
To call and load javascript in struts jsp.
Question on jQuery ajax functionality & troubleshooting called bean in JSF page
wrong jar reference
My bean doesn't quite work