wood burning stoves 2.0*
The moose likes Struts and the fly likes How to select a value in the drop down list with value getting from database? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to select a value in the drop down list with value getting from database?" Watch "How to select a value in the drop down list with value getting from database?" New topic
Author

How to select a value in the drop down list with value getting from database?

sruti roy
Greenhorn

Joined: May 28, 2009
Posts: 5
Hello
i m devoloping struts application and arrived with the problem with setting dropdown list.
i want to set perticuler month or year or other value in the drop down list .these value will come from database...
i have property in bean there i am getting the value in integer...like form month january is 1,feb is 2 so on....now i want to set the values in the list accordingly...such that if value is 1 then in the list the january will be selected.....
can anybody please help me...... ...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Which version of Struts?

If S1, if the value is set appropriately in the form bean then html tags should already be selecting it appropriately. If S2, if the value is set on the action property and the tag is used correctly it should be pre-selected.
sruti roy
Greenhorn

Joined: May 28, 2009
Posts: 5
this is s1.2
i m getting in action class property but unfortunately it is not showing in the html drop down list... here i m posting my code...please tell me where i make the mistake....
//this is jsp..view page
<html:select name="topsheetactionForm" size="1" property="month">
<htmlption value="0">Select Month</htmlption>
<htmlption value="1">January</htmlption>
<htmlption value="2">February</htmlption>
<htmlption value="3">March</htmlption>
<htmlption value="4">April</htmlption>
<htmlption value="5">May</htmlption>
<htmlption value="6">June</htmlption>
<htmlption value="7">July</htmlption>
<htmlption value="8">August</htmlption>
<htmlption value="9">September</htmlption>
<htmlption value="10">October</htmlption>
<htmlption value="11">November</htmlption>
<htmlption value="12">December</htmlption>
</html:select>

//this is bean corresponding to month
public String getMonth() {
return month;
}

public void setMonth(String month) {
this.month = month;
}
//from database i m getting the value of month in the variable...this is my action class
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
topsheetActionForm tpform1 = (topsheetActionForm) form;//object of bean

try{
resultset rst=stmt.executequery(select month from table where condition);//this are done properly...//getting data from database....connecting are done...
while(rst.next()){
tpform.setMonth(rst.getString("month"));
}
}
by this way if i set the ttext box then there is no problem..but it is not selecting the drop down list...
please help....i m stuck in my porject....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags.

You're setting all the options manually--if you're going to create the options manually then you'll have to choose the selected one manually as well.
sruti roy
Greenhorn

Joined: May 28, 2009
Posts: 5
then what is the alternative....how can i get it done...???...how should i do it?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Use the options or optionsCollection tag?
 
GeeCON Prague 2014
 
subject: How to select a value in the drop down list with value getting from database?