aspose file tools*
The moose likes Struts and the fly likes How do I create a table in a jsp and populate it with an ArrayList from an action? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How do I create a table in a jsp and populate it with an ArrayList from an action?" Watch "How do I create a table in a jsp and populate it with an ArrayList from an action?" New topic
Author

How do I create a table in a jsp and populate it with an ArrayList from an action?

Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Hello!

I'm making a webapp for booking conference rooms, using Struts 2.

Right now I need to create a table, with several columns and rows, in a jsp page.
It needs to be populated with an ArrayList, from an Action class (Java class), which contains the information about the bookings(Time, Date, Room number etc.)

My question is: How should I create the table? Should I use Javascript or HTML?
How can I populate it with the info from my ArrayList?

I've looked at several HTML tables aswell as Javascript tables, but I can't see how I can access the ArrayList from my Action class.

If you know about any good tutorials, examples etc, I'd appreciate the links for them.

Here's all the code involved:
ReservationWeekAction.java


ReservationList.jsp


The table in the jsp is far from done, it's just a test to see how I can get values form a bean to be shown inside it.

Thanks in advance,
Alia
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

Please be sure to ask Struts questions in the Struts forum. I have moved this post there for you.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
This was a Struts question?
Hm.. I thought it wasn't, since I'm asking about HTML and javascript code, not struts tags.. Ohwell
Thanks, I guess.

Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Firstly, the business data (information about the bookings) should not be contained by a Struts Action class. This business data should be contained by a simple Plain Old Java Object class.


how I can access the ArrayList from my Action class


This is a pretty simple task.

The part that you seem to be missing is you need to put the ArrayList in the right scope and use the
<jsp:useBean> element.

Code in the JSP file will contain the iteration that will build the HTML table with the data content from the ArrayList.

Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Thanks!
I'm aware of the buisness data being in a POJO, and it is. I guess I didn't formulate my questiong correctly.
What I mean was that I want to access the values of the Array of POJO's (In this case Reservations), which is located in my Action class, not that I want to access the values of my Action class.

Thanks for pointing me into the right direction!
Regards
Alia
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
New question then..
useBean , as the name indicates, uses a bean. But how do I "get" the ArrayList into the jsp, through useBean? That should mean that I need to put the ArrayList in the bean, which sounds whacko to me. What am I missing..

Thanks for bearing with me,
Alia
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Alia Huss wrote:New problem occurring, when I try to use the <jsp:useBean> tag I get this warning: "Unknown tag(jsp:useBean)" I guess it isn't finding the tag library, eh? Can barely find anything on the web about this. Do you have any idea about how to fix it?


'jsp' prefixed tags are standard tags from SUN for a JSP page, No need to import nor have to define any taglib for it.. So if its giving a warning, its ok. See whether its working on page or not


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
*coughs* It works just fine.. I just hadn't realized that I was typing everything in a html document, not a jsp page.


The initial problem still persists though.
I don't know how to put my ArrayList in a scope, and I don't know how to get it in the jsp.

Thanks
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Alia Huss wrote:
I don't know how to put my ArrayList in a scope,


Alia Huss wrote:
and I don't know how to get it in the jsp.

Use struts tag,

Or Use EL

Or Use scriptlet
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Wow thanks! That was helpful indeed!
Though "request.setAttribute("MY_LIST", list);" gets a compilation error,
Should I change my execute method so it takes a HttpServletRequest etc?

Ex.

Thanks
Alia

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Alia Huss wrote:
Though "request.setAttribute("MY_LIST", list);" gets a compilation error,

Explain the error ?
Alia Huss wrote:
Should I change my execute method so it takes a HttpServletRequest etc?

Ex.


I don't know How you are implementing your action class, It will be easier to solve if you post your action class.
But, are you sure the method signature of Action class is correct,

The return type is expected of type 'ActionForward', Look here for correct overriden method for Action#execute()
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Struts 2.

Normally you wouldn't put data like that into scope manually, you'd create an action property and use the <s:iterate> tag. You should probably spend some time looking at some of the S2 documentation wiki information, maybe grab Roughley's free Struts 2 book, and check out some of the example applications that come with Struts 2--it would answer a lot of your questions and save you some time.

If you *really* wanted to put it into scope you're probably implement RequestAware (or, less preferably, ServletRequestAware). You can't just randomly change the signature of a method and expect it to work; something has to *call* your methods and provide an argument. Even the most cursory examination of the documentation or the API would show that nothing in the framework would do this, and a reading of the FAQ would answer the question about how to access tings like the request, session, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I create a table in a jsp and populate it with an ArrayList from an action?
 
Similar Threads
Can I access the value of a listKey in <s:select> from my Action Class?
Problem to save select tag value in struts2
<h:commandLink> is not able to invoke backing bean method
Struts 2: Prepopulating fields in a popup window.
calling the struts action on jquery button click