• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Combo box building

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI All,

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

Thanks
Rudresh
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use html:select and html:optionsCollection.

API can be found here:
http://struts.apache.org/1.2.7/userGuide/struts-html.html
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rudresh:

1.- You have a list objet from database.

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.

4.- In the jsp you must use the next tags:

<html:select property="valueComboList">
<html ption value="">Choose something</html ption>
<html ptionsCollection property="comboList"/>
</html:select>

the attribute valueComboList haves the value that you choose in the ComboBox

I hope that it is useful.


Bye
Nacho Espinosa.
 
rudresh kumar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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">

<html:select name="maritalStatus" property="marital_status_id" multiple="true" >

<html ptionsCollection property="marital_status_list"/>

</html:select>

</html:form>

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

That's my suggest


Bye
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

Kindly help me in solving this issue
Thanks
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Nacho Espinosa
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rudresh:

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
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Example:
<struts-config>
<form-beans>
<form-bean name="YourActionForm" type="your.path.YourActionForm"/>
</form-beans>

<action path="/temp" name="YourActionForm" type="your.path.YourAction" scope="request">
<forward name="jsp" path="/jsp/index.jsp"/>
</action>
</struts-config>

And when you use the Action, set the collection in the ActionForm

Example:

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

YourActionForm yourForm = (YourActionForm) form;

//Here you set your collection
yourForm.setCollection(yourCollection());

return mapping.findForward("jsp");
}


Try with these examples.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic