Javatpoint Logo

how to make dynamic drop down list with struts

By: nitish*** On: Sun Jul 24 05:50:55 IST 2016     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
i want to make a dynamic drop down list with struts jsp and hibernate.If i select the first option it should display the sub list of second drop down taking the data from database. pls help me Up0Down

 
In order to create a dropdown list in struts2 <s:select /> is used on a jsp page. Just define a action name for displaying a dropdown list having value from database. In action class we define a list, and its getter and setter. and in execute method we add the elements in the list from the database.

On returning success page we get all the list elements by OGNL on jsp page as follows:
<s:select name="mydrop_down" list="%{sports}" />

where list is a Iterable source to populate from. If the list is a Map (key, value), the Map key will become ´value´ parameter of option tag and the Map value will become the option tag body.

Whatever you want to create a List/Map/Array in your action class and provide its getter and setter. How the list will be picked form the ActionClass in jsp that will be handled by the framewrok itself.

Action Class MyAction.java:
--------------------------------------
public class MyAction extends ActionSuport{

private List<String> sports; //can be array or map etc

//generate getters and setters for sports

public String execute() throws Exception{
sports = init the List and fill it
// can fill the list from database
return SUCCESS;
}
}

I hope you will understand.
Image Created0Down

By: [email protected] On: Sun Jul 24 14:39:41 IST 2016 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No