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
posted
0
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.
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
posted
0
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
posted
0
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
posted
0
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..
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
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
posted
0
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?
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()
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.