aspose file tools*
The moose likes Struts and the fly likes selecting a value in drop down box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "selecting a value in drop down box" Watch "selecting a value in drop down box" New topic
Author

selecting a value in drop down box

Ketan Parekh
Greenhorn

Joined: Sep 15, 2005
Posts: 20
hi,

I have a jsp page which displays results from database, in that page i have drop down which i populate through a list stored in session object.

I do not know how to make select a value in drop down with the value i retrieve from database.

I am using <html:select>
<html ptions collection=""......>
</html:select>

if anyone can help, its appreciated.

Thanks.
[ July 14, 2008: Message edited by: Ketan Parekh ]
Sujeevan Nagarajah
Greenhorn

Joined: Sep 09, 2007
Posts: 8
Hi,

If you have the value in your ActionForm, Struts will automatically select the drop down list.

<html:select property="myProperty" name="myActionForm">
<html ptions collection="...." labelProperty="label" property="value" />
</html:select>

In this example, if you assign something to 'myProperty' of the ActionForm (You can do it inside the Action class), when request is forwarded to this jsp, the jsp will automatically select the option based on the value inside 'myProperty'.

Hope you understand the solution.

Sujee
Ketan Parekh
Greenhorn

Joined: Sep 15, 2005
Posts: 20
I did not get you. do i need to set it in the Action? If possible can you give me some example.
Sujeevan Nagarajah
Greenhorn

Joined: Sep 09, 2007
Posts: 8
Hi please check this example. I could not test this. Please change if there is any syntax mistakes.
Basically when you submit a jsp with html form element, all html form elements' values are saved inside the FormBean instance, then strut passes this instance to Action class, so you can access this and manipulate. Usually we do this to get values from user and save inside the database.

you want to do it other way around. In this case, first we populate the FormBean with values retrieved from db. Then forward the request to jsp with html form defined. When jsp is rendered, the struts checks the FormBean, if it has values, assign them to html element based on the "property" attribute. This is how you initialize the jsp.




//Action form class.
public class MyForm extends ActionForm {

private String lastName = "";

public String getLastName() {
return (this.lastName);
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}

//Action class.
public class MyAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,HttpServletResponse response) {
MyForm f = (MyForm ) form;

//Retrieve db value and assign it.
f.setLastName = "my_DB_value";

return (mapping.findForward("forwardMe"));
}
}

//my.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head><title>Sample</title></head>
<body>
<html:form action="my.do">
<html:select property="lastName"> //Notice the property name; It is same as //FormBean property
<html ption value="a">value1</html ption>
<html ption value="a">value2</html ption>
<html ption value="a">value3</html ption>
</html:select>
<html:submit/>
</html:form>
</body>
</html>

//Struts-config
<action-mappings>
<action path="/my"
type="MyAction"
name="MyForm"
<forward name="forwardMe" path="/my.jsp"/>
</action>
</action-mappings>
Ketan Parekh
Greenhorn

Joined: Sep 15, 2005
Posts: 20
thanks Sujeevan,

In my case I am calling a DAO class to get db results and return the results stored in list in form of normal java bean class.


if I was returning a jave bean class then I should use Beanutils.copy to copy java bean to actionform bean ? and since i am returning a list, i retrieve the javabean class from list in jsp and then assign values using expression <%=%>, than how to select the drop down list value.

Thanks,
Ketan.B.Parekh
Sai Narasimha Reddy
Greenhorn

Joined: Dec 13, 2006
Posts: 23
I think that's not the way it should be done. I mean, what if the form you are populating in another Action has some validation bound to it in validator-rules.xml?? in that case that form will have to be validated before it's sent to the Action ( in which you're setting the drop-down list), which fails because no values will exist for the form-fields.

I suggest you use following approach.




this solution is similar to the above solution in passing the request through a Action. But i've created a request scope variable with my values to be displayed in the JSP rather than associating a form with the action and directly filling the form with values.

get it??

I'm happy to answer any queries!


Sai Narasimha
 
wood burning stoves
 
subject: selecting a value in drop down box