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

dynamic drop down list

Kan Qiu
Greenhorn

Joined: Nov 26, 2002
Posts: 16
I need to get an id from jsp session Object, and pass this id to some other class and use it to get some data from database, and then display those data in an drop down list only if the user select an option from an existing dropdown list.
I used following code to get the id in the JSP.
<%
User user = (User)request.getSession().getAttribute("user");
String id = user.getID();
if (user.isDemoUser())
id = "483297701";
%>
I have a DAO class with method like
public Vector findAllData(String id)throws Exception{}
in my Form class, I did this
public class VerificationsForm {

private String selectedVerification;
private String selectedSession;

private String id;
private String[] sessionValues;
private String[] sessionLabels;
public void setSessionValues(){
VerificationDAO sgrDAO = VerificationDAO.getInstance();
Vector vec = null;
try{
vec =sgrDAO.findAllSessions(getId());
}
catch(Exception e){}
finally{
if (vec!=null && vec.size()>0) {
int s = vec.size();
sessionValues = new String[s];
vec.copyInto(sessionValues);
}
}
}
public String[] getSessionValues(){
if (sessionValues==null) setSessionValues();
return this.sessionValues;
}
public void setSessionLabels(){
String[] temp = getSessionValues();
int s = (temp==null?0:temp.length);
sessionLabels = new String[s];
for (int i=0; i<s;i++){
sessionLabels[i] = formatSession(temp[i]);
}
}
public String[] getSessionLabels(){
if (sessionLabels==null) setSessionLabels();
return this.sessionLabels;
}
public void setId(String value){ this.id=value; }
public String getId(){ return this.id; }
public void setSelectedVerification(String value){ this.selectedVerification=value; }
public String getSelectedVerification(){ return this.selectedVerification; }

public void setSelectedSession(String value){this.selectedSession=value;}
public String getSelectedSession(){return this.selectedSession;}
}
The problem is: It seems that the id that I need to ne used in the Form object can not be passed, it alsway get null value? so, this may not a good design to deal with the case. Any help is welcome.


Kan Qiu<p>SCJP<br />SCWCD
David Yutzy
tumbleweed and gunslinger
Ranch Hand

Joined: Jun 29, 2001
Posts: 192
The way that I handle this is have your dropdown onClick post to an DispatchAction servlet updateDropdown() method (using DispatchAction is much preferred to handle your situation).
You then read the posted value from the form, populate the list of the dropdown using your DAO call, then forward back to the JSP.
Stuart Austin
Greenhorn

Joined: Dec 13, 2003
Posts: 1
Interesting, but I do not understand how the action can forward back to the refering action.
David Yutzy
tumbleweed and gunslinger
Ranch Hand

Joined: Jun 29, 2001
Posts: 192
First of all, I generally use Form beans to simply pass and receive information from JSP forms, meaning, no business logic in the form bean.
Let me walk you through the scenario:
* Action.do, method init() is called and populates the first dropdown.
* Forwards to .jsp file and form fields are displayed.
* User selects an item from the dropdown.
* Javascript in onClick method changes the hidden field named "method" to
the value of "update2DD"
* Javascript posts form to Action.do, method update2DD(). This method is \
called automatically when using DispatchAction.
* update2DD() method takes value posted from field for dropdown and
populates second dropdown list.
* Same method as was called in init() re-populates first dropdown list.
This is required since only the selected value is posted, not the entire
list. Since the posted value still remains in the form bean, it will
automatically be defaulted when form is redrawn.
* findForward should point to .jsp file where the form is re-drawn and
the second dropdown is displayed.
The key to making this all work is using DispatchAction versus regular action servlets. Yes, you could do it, but you'd have to do some fancy URL/logic to make it happen. DispatchAction makes complex form/actions a snap.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic drop down list