aspose file tools*
The moose likes Struts and the fly likes Problem in retrieving List from class and displaying it in JSP in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem in retrieving List from class and displaying it in JSP in struts2" Watch "Problem in retrieving List from class and displaying it in JSP in struts2" New topic
Author

Problem in retrieving List from class and displaying it in JSP in struts2

Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23
hello everyone...m using struts 2 to make a dynamic menu from database....
following are the contents of my project




menujsp.jsp


index.jsp :



web.xml:



struts.xml :



ConnectionProvider.java :


DAO.java :



MenuList.java :




MenuListAction.java :




plzz if anyone can help me out with my jsp...how to use the list in jsp's iterator...

thanks in advance!!!

Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

Welcome to the JavaRanch. I deleted the duplicate post you made in the JSP forum. Posting the same question more than once causes confusion and duplication of effort as the community tries to help everyone.
I also added code tags around your code. This helps preserve formatting making your code easier to read. You should spend some time with our FAQ, HowToAskQuestionsOnJavaRanch. The better question you ask, the more help we can be.
When you run this code, what happens? I see a big problem in the way you are doing submenus. How about simplifying your code for now and just try to print out a list of links? Once you get that working, you can try something more complex.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

<s:iterator value="%{menulistlist}" var="menulist">
<tr><td class="menu" id="<s:property value="%{menuId}"/>"><s:property value="%{submenuTitle}"/></td></tr>
</s:iterator>


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

Mohan Rao Sv wrote:<s:iterator value="%{menulistlist}" var="menulist">
<tr><td class="menu" id="<s:property value="%{menuId}"/>"><s:property value="%{submenuTitle}"/></td></tr>
</s:iterator>


The "%{}" is the expression escape sequence. A string value set on a string attribute would be evaluated as a string, so it is really unnecessary.
OGNL Basics
Struts Tag Syntax
Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23
thanks joe...i've jst joined here few days back...din't knew much about hw to post question


i'm trying to simplify dis now...if its working or not...i'm not using style or any script here...here's the simplification i did in my jsp :-



still not getting anything....i'm getting following in my console:

Inside DAO fetch...

statement Prepared....

Executing 1st query...

Inside setmenuId of MenuList class..set menuId= 0000000001
Id is 0000000001

inside while of DAO,setting menuId as... 0000000001 0000000001

Inside setmenuTitle of MenuList class..set menuTitle= Registration
menu title is Registration

inside while of DAO...setting menuTitle as... Registration Registration

Inside setmenuId of MenuList class..set menuId= 0000000002
Id is 0000000002

inside while of DAO,setting menuId as... 0000000002 0000000002

Inside setmenuTitle of MenuList class..set menuTitle= Renewal
menu title is Renewal

inside while of DAO...setting menuTitle as... Renewal Renewal

Inside setmenuId of MenuList class..set menuId= 0000000003
Id is 0000000003

inside while of DAO,setting menuId as... 0000000003 0000000003

Inside setmenuTitle of MenuList class..set menuTitle= EMI
menu title is EMI

inside while of DAO...setting menuTitle as... EMI EMI

Inside exceute of MenuListAction..
Inside getMenulistlist of MenuListAction...
Inside getMenulistlist of MenuListAction...
Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23

i'm just getting the table headers...not the values under it...

regards!!!
Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23
i have resolved the problem to some extent..there was a big problem with DAO.java..earlier by mistake i have commented on ....now its working...the values are coming in under MenuID...but not under the MenuTitle column...


menujsp.jsp:




here's the output of my console...:-

Inside DAO fetch...

statement Prepared....

Executing 1st query...

Inside setmenuId of MenuList class..set menuId= 0000000001
Inside getMenuId() of MenuList class...menuId is 0000000001

inside while of DAO,setting menuId as... 0000000001 0000000001

Inside setmenuTitle of MenuList class..set menuTitle= Registration
Inside getmenuTitle() of MenuList class...menu title is Registration

inside while of DAO...setting menuTitle as... Registration Registration

Inside setmenuId of MenuList class..set menuId= 0000000002
Inside getMenuId() of MenuList class...menuId is 0000000002

inside while of DAO,setting menuId as... 0000000002 0000000002

Inside setmenuTitle of MenuList class..set menuTitle= Renewal
Inside getmenuTitle() of MenuList class...menu title is Renewal

inside while of DAO...setting menuTitle as... Renewal Renewal

Inside setmenuId of MenuList class..set menuId= 0000000003
Inside getMenuId() of MenuList class...menuId is 0000000003

inside while of DAO,setting menuId as... 0000000003 0000000003

Inside setmenuTitle of MenuList class..set menuTitle= EMI
Inside getmenuTitle() of MenuList class...menu title is EMI

inside while of DAO...setting menuTitle as... EMI EMI

Inside exceute of MenuListAction..
Inside getMenulistlist of MenuListAction...
Inside getMenuId() of MenuList class...menuId is 0000000001
Inside getMenuId() of MenuList class...menuId is 0000000001
Inside getMenuId() of MenuList class...menuId is 0000000001
Inside getMenuId() of MenuList class...menuId is 0000000002
Inside getMenuId() of MenuList class...menuId is 0000000002
Inside getMenuId() of MenuList class...menuId is 0000000002
Inside getMenuId() of MenuList class...menuId is 0000000003
Inside getMenuId() of MenuList class...menuId is 0000000003
Inside getMenuId() of MenuList class...menuId is 0000000003
Inside getMenulistlist of MenuListAction...
Inside getMenuId() of MenuList class...menuId is 0000000001
Inside getMenuId() of MenuList class...menuId is 0000000002
Inside getMenuId() of MenuList class...menuId is 0000000003



the problem according to me is when i'm putting data in table through ...it should call getMenuTitle() of MenuList.java class...but thats not happening here....thats why its not putting values under the column of MenuTitle...

Hope someone helps me out with this...
thnksss :-) !!!
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

For your information AccessStrutsValueStack
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

Your getters and setters aren't Camel Case:


Compare to this getter:


The names of getters and setters are dictated by the JavaBean specification which allows us to use reflection to look up object properties at run time.
Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23
Thanks Joe Ess ....its working now...i didn't knew anything regarding what you've told me...
thanks for your help....:-) !!!
Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23





now i'm trying this in my JSP....but 2nd iterator is not working....or its not taking the valuse in menuId and menuTitle inside 2nd iterator.....
how can i correct this thing...plzzz hepl..
regards!!!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

I don't think you have this functionality thought out. You want to have a list of menus with sub-menus, right? Why are you trying to iterate through the same list of menus twice? This is what I see when I look at your code:


What you should probably have is a list of menu items and each menu item having a list of sub-menus. When you iterate through the menu lists, use the second iterator to invoke the getter for the sub-menu list.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

simi nair,
Your post was moved to a new topic.
Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23
Hello Joe Ess ....you are right that i am iterating the submenus with same list of menuTitle..i did this just to check if my second iterator(the nested one) is working or not...
after that i will do the project that is given to me in which i have to add subMenuTitle in the list in DAO.java with the help of a new query...where i will add the subMenuTitle for the its corresponding menuId....
how will i do it i am aslo not sure about that as well!!!
Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23
this is what i want my menu to show:

menuTitle;- (Regsitration) (Renewal) (EMI) when i'll do mouse over on them they will show

(Direct regist) (Renew Registration) (ER1 From)
(Re registration) (Record of Renewal) (ER2 From)

i've done this successfully in single JSP in which evrything resides in JSP,following is that code of JSP:




now i want this to do be implemented in STRUTS2....


Following are my tables :


application table

application_id title short_title

(0000000001) (Registration Module) (Registration)
(0000000002) (Renewal Module) (Renewal)
(0000000003) (EMI Module) (EMI)



menu table:

[b](menu_id) (title) (short_title) (menu_typecharacter)

(0000000001) (Direct Candidate Registration) (Direct Regist) (H)
(0000000002) (Re registration) (Re registration ) (H )
0000000003 Renew Registration Renew Registration H
0000000004 Record of renewal Record of Renewal H
0000000005 ER1 Form ER1 Form H
0000000006 ER2 Form ER2 Form H



applicationmenu table:

application_menu_id application_id menu_id

0000000001 0000000001 0000000001
0000000002 0000000001 0000000002
0000000003 0000000002 0000000003
0000000004 0000000002 0000000004
0000000005 0000000003 0000000005
0000000006 0000000003 0000000006


1ST QUERY: select * from application where application_id in (select distinct application_id from applicationmenu order by application_id)
RESULT:
this goes in my ResultSet rs

application_id title short_title

0000000001 Registration Module Registration
0000000002 Renewal Module Renewal
0000000003 EMI Module EMI


2ND QUERY: "select * from menu where menu_id in (select menu_id from applicationmenu where application_id='"+rs.getString(1)+"')")


When rs.getString(1) will be 0000000001 then it will show the following result :

menu_id title short_title menu_typecharacter
0000000001 Direct Candidate Registration Direct Regist H
0000000002 Re registration Re registration H

Hence it will generate the sub-menu for menuId=0000000001

And so on for menuId=0000000002 and 0000000003


all this will be in another ResultSet...

this is what my project is all about...plzzzz if you can suggest me what should i do....
thanks a lot!!!


Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

As I said before:
Joe Ess wrote:
What you should probably have is a list of menu items and each menu item having a list of sub-menus. When you iterate through the menu lists, use the second iterator to invoke the getter for the sub-menu list.


The way the iterator tag works is that it takes an object out of the collection it is processing and pushes it onto the value stack. In your case, that would be an instance of MenuList. The second iterator attempts to reference a getter "menulistlist", which MenuList does not have.
Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23
Hello Joe Ess ...
i have tried the following changes in my classes:


in DAO.java i have added a function that executes the 2nd query for submenus...





in MenuListAction class i have added :



in execute method i hv added the following :



and have added hetters n setthers for submenuTitle in MenuList class :






and menujsp as:









now i am getting the submenus...but the problem is that i am getting all the submenuTitles under every menuTitle... is there a way that i can get the value ftom the JSP n pass it the query as an action...for a particular menuId n then i can execute the query for that menuId...select * from menu where menu_id in (select menu_id from applicationmenu where application_id="for that menuId")

and then com back to second iterator n display submenuTitle for that menuId on mouseover event
or is there any other way to achieve what i am trying....
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

Sachin Negi wrote:
now i am getting the submenus...but the problem is that i am getting all the submenuTitles under every menuTitle.


That's how you have it coded. How can you associate a submenu with a menu?
Sachin Negi
Greenhorn

Joined: Feb 02, 2012
Posts: 23
so how can i correct myself??? can i do changes in JSP so that i can get submenuTitle under the menuId for which it is associated???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in retrieving List from class and displaying it in JSP in struts2