Problem while trying to dynamically populate drop down menu <select> in jsp (am using struts 2 framework)
(Sorry for the length of the post!!)
edit:
By dynamically I mean--> when the selection of a drop down changes I want the contents of other drop downs to change as well. Thanks!!
I am trying to dynamically populate dropdown menu in jsp (I guess this is a very common question). I am using struts 2 framework.
I found some solutions by googling and from some books but most of them required a lot of scripting in the jsp page, which I dont want to do, cause I think it is not a good practice.
I was hoping to find a way where I could call an action from an onChange event where all the coding part could be done (ofcourse some scripting will be used :) ) .
开发者_开发问答One way I found was by using dojo. I implemented it and its working fine except for 2 problems:
The action gets called on loading of the page itself, even when the selection of any of the drop downs has not changed.
Following error message is displayed just above the form which has the drop down menus --> "Error loading '/GetLists.htm' (500 Internal Server Error)".
One more question I want to ask is that wether this is a good way to implement dynamically populated drop downs or not. And is my notion of avoiding scripts on jsp pages right or not.
Here are the codings:
The jsp page:
<s:form id="lists" action="viewDayReport">
<s:url id="scriptURL" action="GetLists"/>
<sd:div listenTopics="getLists" href="%{scriptURL}" formId="lists" showLoadingText="Working..."/>
<s:select label="Customer " name="customer" headerKey="0" headerValue="Select" list="customerList" onchange="dojo.event.topic.publish('getLists');return false;"/>
<s:select label="Contact " name="contact" headerKey="0" headerValue="Select" list="contactList" onchange="dojo.event.topic.publish('getLists');return false;"/>
<s:select label="Employee " name="employee" headerKey="0" headerValue="Select" list="employeeList" onchange="dojo.event.topic.publish('getLists');return false;"/>
<s:select label="Stage " name="stage" headerKey="0" headerValue="Select" list="stageList" onchange="dojo.event.topic.publish('getLists');return false;"/>
<s:select label="Type " name="type" headerKey="0" headerValue="Select" list="typeList" onchange="dojo.event.topic.publish('getLists');return false;"/>
<sd:datetimepicker label="Date" name="date" displayFormat="dd/MM/yyyy" />
<s:submit value="View Report(s)"/>
</s:form>
This is the struts config file:
<struts>
<package name="Deutek.admin" extends="struts-default" >
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
//this is the action that is executed when the page first loads. It populates the lists for drop downs
<action name="dayReportPage" class="admin.dayReportAction">
<result type="tiles">dayReport</result>
</action>
//this is the action that is executed when a drop down selection is changed. currently the action just prints some output.
<action name="GetLists" class="admin.GetListsAction">
<result type="tiles">dayReport</result>
</action>
</package>
</struts>
I would suggest ether 1. populating it from your struts action class or 2. via static method invocation at the jsp level.
If you want to set the lists up in your struts action, you can do it by implementing Preparable in the action:
public MyAction extends ActionSupport implements Preparable{
private List customerList;
public void prepare(){
customerList= CustomersDAO.getCustomerList();
}
// Getters and Setters
}
You can then get the list from the jsp like this:
<s:select label="Customer " name="customer" headerKey="0" headerValue="Select"
List="customerList"/>
To set up dynamic method invocation you can access the CustomersDAO object from the previous example directly something like this:
<s:select label="Customer " name="customer" headerKey="0" headerValue="Select"
List="@com.mypackage.CustomersDAO@getCustomerList()"/>
To enable static method access set the struts2 constant in the struts.properties file:
struts.ognl.allowStaticMethodAccess=true
All you need is a doubleselect tag.
Here's a good example.
精彩评论