• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

selecting a value in drop down box

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic