*
The moose likes Struts and the fly likes Problem in populating data with html:select Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem in populating data with html:select" Watch "Problem in populating data with html:select" New topic
Author

Problem in populating data with html:select

Vicky Mohan
Ranch Hand

Joined: Oct 14, 2004
Posts: 130
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
James Zhang
Ranch Hand

Joined: Sep 06, 2002
Posts: 30
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

Joined: Oct 14, 2004
Posts: 130
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

Joined: Sep 06, 2002
Posts: 30
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

Joined: Oct 14, 2004
Posts: 130
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

Joined: Oct 14, 2004
Posts: 130
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.
Pranav Sharma
Ranch Hand

Joined: Oct 27, 2003
Posts: 258
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
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
"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

Joined: Oct 14, 2004
Posts: 130
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.
Srilakshmi Vara
Ranch Hand

Joined: Jul 21, 2004
Posts: 169
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

Joined: Oct 14, 2004
Posts: 130
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

Joined: Jul 21, 2004
Posts: 169
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

Joined: Oct 14, 2004
Posts: 130
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

Joined: Jul 21, 2004
Posts: 169
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

Joined: Oct 14, 2004
Posts: 130
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 ]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
Vicky Mohan
Ranch Hand

Joined: Oct 14, 2004
Posts: 130
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

Joined: Oct 27, 2003
Posts: 258
it should work, if you set it to request scope
Michael Sweeney
Greenhorn

Joined: Nov 01, 2004
Posts: 5
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 ; }



}


mesas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in populating data with html:select