• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in populating data with html:select

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

I am trying to populate a select box using html:select. I have the collectin in request and i am able to populate using html ptions. But i am not sure how to prepopulate the select box with a value available in request.I set the value attribue with suitable value, but html:select never seems to recognize that.

I have attached the code below .

<B>
<html:select property="selectBox" value='<c utvalue="${reqObj.id}"/>' style="WIDTH: 180px" >
<html ption value="">Select One</html ption>
<html ptions collection="description" property="id" labelProperty="siteName"/>
</html:select>
</B>

Any help will be greatly appreciated
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way to do it is to define the properties in your struts form for the values and labels. For example, you can define a property called "values" as a collection of the desired values and another property called "labels" as the desired labels for your struts form, then the following codes in your jsp file should display the list:

<html:select property=...>
<html ptions property="values" labelProperty="labels" />
</html:select>


Good luck!
 
Vicky Mohan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

I have no issues in populating the collection in the select box.

My issue is to get the correct value as selected.

I apologize if i was not clear the first time.

<html:select property="test" value="<c ut value='${reqObj.id}'/>" style="WIDTH: 180px" >

I want suggestion how to set the VALUE attribute in the above code.

Any suggestions will be apprecaited

[ August 19, 2005: Message edited by: Mohen Vijay ]
[ August 19, 2005: Message edited by: Mo Vj ]
 
James Zhang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to use a certain value as the default selection whenever the form is displayed, set the value for your form property "inmateSite1" inside the method reset().
 
Vicky Mohan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, i have the entire object available in request.

All i need to do is set the appropriate value ( from the object availble in request) to the select box.

So, do you mean to suggest that i cannot set the value using the attribute VALUE ???

My guess is, i am doing something wrong in the JSTL . When i hardcoded to a numeric value, html:select does recognize and populate the correct value. All i need to know is , how should i use the VALUE attribute ???

All suggestions are welcome
 
Vicky Mohan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still looking for some help in pre populating the Select Box with the selected value.

Suggestions would do a world of good to me

Thanks for your help.
 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i understand you correctly, you make a list of dynamic options for a select box and you want a value z to be selected to be shown.

if yes, then add a field called "selectBox", set its value with what you need selected.

then in your jsp
html:select property="selectBox" >
<html option value="">Select One</html option>
<html options collection="description" property="id" labelProperty="siteName"/>
</html:select>

this value should be equal to one of the values of the options and that option will be selected
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Mo Vj",

Welcome to JavaRanch. We don't have many rules here, but we do have a naming policy which we try to strictly enforce. Please re-read this document and edit your display name in order to comply. As a side note, your previous display name, "Mohen Vijay", was in compliance with our policies ("Vj" is an obviously fictitious name). Thanks and we look forward to your continued participation at JavaRanch.
 
Vicky Mohan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, i do know how to populate the drop down, i was just wondering if i can use the JSTL c ut to set the value of the select box.
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to see in the html generated by the struts if it shows the exact number you are expecting then struts will automatically highlights that value..

Or try using form.setSelectBoxValue in the Action class before populating jsp.

Srilakshmi
 
Vicky Mohan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain how we can re populate the actionForm in the Action class ?

I have the scope as REQUEST and i do not want the form in session scope.

Thanks for your help
 
Srilakshmi Vara
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before displaying the jsp you need to write a Action Class which populates the form, Then create a action mapping and then direct to jsp on success.
 
Vicky Mohan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, maybe, i was not clear.

I do have the populateAction in place, and i was wondering if i need to set the ActionForm in request ?? I tried to set values in ActionForm but my JSP does not pick up the values set.
 
Srilakshmi Vara
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will you send me some more details like your action class, action form and mapping entry in config file?

FYI
If you set your form scope as request for every action your form will reset and you lost the state of your form.

Srilakshmi
 
Vicky Mohan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, i am aware of the fact that setting the form scope to request resets the values. Per my requirements, i prefer to have the scope of the form as REQUEST rather than SESSION.

I have four form variables ( input text box) and a select drop down box. On submission, i make a search and do some business logic. After that i get redirected to the same JSP. I have an object ( thaat has all the values entered through the UI ) available in request.

I try to use the C UT ( JSTL ) and it works fine for all input text boxes.

Only issue is for the drop down box. I cannot get the correct value selected.

My conclusion is , maybe, we might not be able to use c ut in html:select.

STRUTS-CONFIG.XML

**********************
<form-beans>
<form-bean name="XXXXXForm" type="xxx.xxxx.xxx.xx.xxxx.actionForm.xxxSearchForm"/>
</form-beans>

<action-mappings>
<action path="/xxxSearchPopulate"
name= "XXXXXForm"
type="xxx.xxx.xxx.xxxxxx.xxx.action.xxxxSearchPopulate"
scope = "request">
<forward name="Success" path="/jsp/xxxxx/XXXXXXXX.jsp"/>
</action>
</action-mappings>

**********************

Any thoughts ?
[ August 22, 2005: Message edited by: Mohen Vijay ]
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mohen Vijay:
I have an object ( thaat has all the values entered through the UI ) available in request.



If that object were the ActionForm, none of this would be an issue because you would not have to specify the tags' value attribute and Struts would take care of all this prepopulation for you.
 
Vicky Mohan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that object were the ActionForm, none of this would be an issue because you would not have to specify the tags' value attribute and Struts would take care of all this prepopulation for you.

Well, One quick question :

Do i need to set the form in request and if so, does that need to be done in the same name as form name

If the form is TestForm,

request.setAttribute("TestForm", testForm);
[ August 22, 2005: Message edited by: Mohen Vijay ]
 
Pranav Sharma
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it should work, if you set it to request scope
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RE: setting the default selection of an html:select. You can set the default value of the form bean's corresponding (corresponding to the html:select, that is) property. But if the act of selecting is supposed to trigger something (eg: populating another list), you've still got a problem.

Here's my select:
*************************************************************************
<html:select styleId="fSelect" property="layer" onchange="showFields()">
<html ptions property="listLayers"/>
</html:select>
*************************************************************************
Here's the form bean:
*************************************************************************
public class FormFields extends ActionForm{
private List listLayers = new ArrayList();

public FormFields(){
super();
listLayers.add("Agricultural Features");
listLayers.add("Agricultural Fields");
listLayers.add("Open Space");
// etc.

}
private String layer= "Open Space"; // <--- NOTICE
private List fieldResults = null;

public void setLayer(String layer) { this.layer = layer; }
public String getLayer() { return layer; }

public void setFieldResults(List fieldResults) { this.fieldResults = fieldResults; }
public List getFieldResults() { return fieldResults; }

public void setListLayers(List listLayers) { this.listLayers = listLayers; }
public List getListLayers() { return listLayers ; }



}
 
reply
    Bookmark Topic Watch Topic
  • New Topic