I have a list object which is returned from database/cache, i need to take this list object iterate it and build a combo box (like apple, mango...). i would like to know how this can be done using logic:iterator or nested tags. kindly give me an example
2.- You transform this list in a Collection but adding the LabelValueBean (see Struts API), for each value about your list.
Example: Collection retCol = new ArrayList(); // Collection that haves the values. [one,two, three] // imagine that it is a list from database for(int i = 0; i < list.length; i++) { LabelValueBean lvb = new LabelValueBean(); lvb.setLabel(list[i]); //value obtained from database lvb.setValue(i); //id about label from list retCol.add(lvb); }
3.- In your ActionForm define a Collection attribute, this attribute must have get and set. example: private Collection comboList;
public Collection getComboList();
public void setComboList(Collection comboList);
3.- set the list in the attribute before you return to jsp.
the attribute valueComboList haves the value that you choose in the ComboBox
I hope that it is useful.
Bye Nacho Espinosa.
Greetings<br />Nacho Espinosa<br />SCJP 1.4
rudresh kumar
Ranch Hand
Joined: Jan 04, 2006
Posts: 82
posted
0
HI,
I am attaching the code, Kindly help me in building combo box in jsp using html select 1.public List getMaritalStatus() { Session session = HibernateUtil.currentSession(); List list=session.createQuery( "from maritalStatus") .setCacheable(true) .list(); HibernateUtil.closeSession(); return list;
} 2.public class maritalStatus {
private String marital_status_id; private String description; private String lastdate; with get and set methods 3.currently i invoke the method using servlet to bring data to cache 4.Now i want to create a jsp which will use the the data from my cache and build a combo box Like (single,divorced....)
Thanks
rudresh kumar
Ranch Hand
Joined: Jan 04, 2006
Posts: 82
posted
0
HI all,
I had done as suggested
HI all,
i have done as suggested, following is the code 1.i have a action class which sets the list value from database to cache (this is working fine) 2.Bean with all get set methods public void setMarital_status_list(Collection statusList) { marital_status_list=statusList; } public Collection getMarital_status_list() { return marital_status_list; 3.JSP code <html:form action="/temp.do" name="maritalStatus" type="com.tcs.des.cache.maritalStatus">
4. the error i am getting is E SRVE0026E: [Servlet Error]-[Failed to obtain specified collection]: javax.servlet.jsp.JspException: Failed to obtain specified collection at org.apache.struts.taglib.html.OptionsCollectionTag.doStartTag(OptionsCollectionTag.java:222)
KIndly help me in solving this problem> I have spent a whole day debugging the same
Thanks in advance Rudresh
Nacho Espinosa
Ranch Hand
Joined: Jan 17, 2007
Posts: 30
posted
0
Hi:
The list that you obtain from database is a bean of hibernate.
But you don�t convert the list a LabelValueBean and add a new list or collection, then when you pass the bean to JSP instead of property marital_status_list. The tag doesn�t recognize the object of hibernate and throw an exception.
Try to convert the objects of hibernate in LabelValueBean.
Is the method getMarital_status_list() on your action form?
You do not have to use LabelValueBean. Instead you can use the label and value attributes of the optionsCollection tag. Something like this might work:
- Brent
rudresh kumar
Ranch Hand
Joined: Jan 04, 2006
Posts: 82
posted
0
HI,
This code is working now if i put the collection in some scope (request or session) and the same scope name i am using in <optionscollection>
if i comment this code (putting in session or request) it is not working
already this data is there in my memory (L2 cache of hibernate) again i don't want to set attribute because again it is a memory usage.
i can some one help me out in solving this issue, data is there in my bean but not populating in the jsp
Thanks Rudresh
rudresh kumar
Ranch Hand
Joined: Jan 04, 2006
Posts: 82
posted
0
HI Ranchers,
This is my humble request, i am not being able to solve this problem from 2 days, The bean is having the values but i am not able to get that in the jsp (because the collection is null in the jsp).
I am again listing my entire code 1.Action class try {List list = this.getMaritalStatus(); ArrayList status =new ArrayList(); Iterator i = list.iterator(); maritalStatus sts= new maritalStatus(); int k=0; while(k<list.size()) {sts=(maritalStatus)list.get(k); status.add(sts.getMarital_status_id()); k++; } sts.setMarital_status_list(status); }
catch (Exception ex) { throw new ServletException(ex.toString()); }finally {return mapping.findForward(target); } } public List getMaritalStatus() { Session session = HibernateUtil.currentSession(); List list=session.createQuery( "from maritalStatus").setCacheable(true).list(); HibernateUtil.closeSession(); return list; } 2.My bean private String marital_status_id; private Collection marital_status_list; private String description; which has get and set methods 3.My jsp <html:select property="marital_status_id" name="maritalstatus" multiple="true" > <html ptionsCollection name="maritalstatus" property="marital_status_list" value="marital_status_id" label="description"/> </html:select> 4.my struts config are correct 5.error i am getting is [Servlet Error]-[Failed to obtain specified collection]: javax.servlet.jsp.JspException: Failed to obtain specified collection This is because the collection is having null value
Originally posted by rudresh kumar: This code is working now if i put the collection in some scope (request or session) and the same scope name i am using in <optionscollection>
You must have the object in a scope in order to use it in a Struts tag. There is no other way.
Originally posted by rudresh kumar:
already this data is there in my memory (L2 cache of hibernate) again i don't want to set attribute because again it is a memory usage.
Your concerns about memory usage are unfounded. When You put the item in a scope, you are not creating a new copy of the item, but simply giving the scope object (request or session) a reference to your object. There is no additional memory usage. The only thing to watch out for is that session scoped objects should be cleaned up after use with the session.removeAttribute() method so that the object can become eligible for garbage collection.
How do you put the collection in request or session?
Do you put the collection,ActionForm or Bean in request?
I think that collection is null because you don�t match anything between the jsp and ActionForm.
Are you defining ActionForm in struts-config.xml?
Check it your struts-config.xml.
rudresh kumar
Ranch Hand
Joined: Jan 04, 2006
Posts: 82
posted
0
HI,
If i put the collection (actionform bean) in the request scope then my jsp is getting value.
i set this collection in my Action class. Let me summarise 1.On request of a page i run a query to get the data from database and that is put in the cache (stored as list object) 2.then i create a instance of my form bean, create a array list and add all the list object(step 1) to this array list and set this. 3.now if i request the jsp mentioning the same form bean then my drop down shld be generated, but it is giving error saying that my array list object is null, i have checked in action class it has values. 4.now i did one change i added that form bean in the request scope and now it is working fine is this the correct procedure, my question is if the form bean has the value why to set it in request scope.
Rudresh
Nacho Espinosa
Ranch Hand
Joined: Jan 17, 2007
Posts: 30
posted
0
Hi rudresh:
When you put the ActionForm in the request it's working.
But you don�t use struts correctly.
The purpose is that struts put the ActionForm in request or any scope, but don�t you.
Then, You must check your configuration in struts-config.xml.