• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

dynamic drop down list

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
tumbleweed and gunslinger
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting, but I do not understand how the action can forward back to the refering action.
 
David Yutzy
tumbleweed and gunslinger
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic