aspose file tools*
The moose likes Struts and the fly likes First struts app help! Drop-down lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "First struts app help! Drop-down lists" Watch "First struts app help! Drop-down lists" New topic
Author

First struts app help! Drop-down lists

jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Hi,
I am doing my first struts app, and I am very confused about everything: I have 3 drop-down lists, I have to populate them before user selections and submission. So, there are two things I have to do: pre-populate the lists, and handle user selections and submission. I try to do these things in one jsp, one actionForm and one Action class. But I have not been successful. I even cannot prepopulate the lists.
I am wondering if I need to define two pages, forms and actions to handle two things separately?
I am using collections to pre-populate lists, I use <html ptions> in my JSP page to get data. But how can I define my actionForms for these collections?
Its just driving me crazy!
regards,
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

This can be very confusing the first time and there are a number of ways the tags behave depending on what combination of attributes you use so this doesn't help the beginner very much.
I just found what looks like a good tutorial on the web: http://www.reumann.net/do/struts/main
In Lesson II, you'll find an example of how to use the html:select tag


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Just a few notes to help you through Lesson II:
Lesson II-4 Create ActionForm - The UI element for entering a value into the department field will be a drop-down list.
Lesson II-6 Create DepartmentBean - This bean will be used to represent options for the drop-down list.
Lesson II-8 Create EmployerService - shows how the list of departments is created.
Lesson II-9 Create SetUpEmployerAction - this is where the list of options is put into the session context.

Lesson II-14 Create employeeForm.jsp - shows how the html:select and htmlptions is used to present a drop-down list of departments.
Note the property and labelProperty attributes in the htmlptions tag: both id and description are properties of the DepartmentBean created in II-6. The collection attribute is the name "departments" which was used when the list of DepartmentBeans was added to the session context (II-9).
Hope this helps.
[ July 20, 2003: Message edited by: Junilu Lacar ]
rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Thank you so much, Junilu.
Your pointers are so wonderful to me as a newbie.
I really appreciate your help!
regards,
Jim
jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Hi, Junilu:
I still need your enlightment.
I can make one drop-down list work, but when I add
other two drop-down lists, I wa told the actionForm
cannot be created.
Here is what I did: for example, I define company, department, employee in my only actionForm, set up getters and setters. I created CompanyBean, DepartmentBean, and EmployeeBean for each drop-down list. In my action class, I get three collections and put them into session context
What did I do wrong? Is one actionForm ok for 3 dropdown lists? Or I need 3 action forms to do the job? Is it possible for one action class to pass 3 action forms?
regards,
Jim
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

You should be able to have any number of drop-down lists on a single form. What is the error you are getting? Please post the relevant portions of your JSP and ActionForm so we can check for any mistakes.
jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Originally posted by Junilu Lacar:
You should be able to have any number of drop-down lists on a single form. What is the error you are getting? Please post the relevant portions of your JSP and ActionForm so we can check for any mistakes.

Thanks, Junilu:
I found my mistake for this error: a typo for my
bean method (Yikes). Thanks for your kind help.
regards,
Jim
jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Hi, Junilu:
More questions. I am now trying to populate a dependent list based on the selected item from the first list. If I only pre-populate the company and department lists, and do not prepopulate employee list.
When I select an department item, the employee list should be populated. My actionForm includes company, dept, and emp variables, and getters/setters. I can use actionForm to send the selected dept to action class(will this work?), then get results from database. But how can I pass the results back to jsp page?
Should I create another action class and action form for this task?

regards,
Jim
[ July 22, 2003: Message edited by: jim yin ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Originally posted by jim yin:

Should I create another action class and action form for this task?

You could, if that's how you want the flow of your application to be. You could also handle this with the same JSP/Action/ActionForm. It really depends. Describe the flow you envision and we'll help you hash out what you need to do. The separate form for the Employee List may be a little less complex solution though and will make for easier maintenance.
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Hi, Jim:
check this web site out: http://www.mattkruse.com/javascript/dynamicslelectbox/
Note that the site will show Status 404 first (the site is not properly set up), just wait a second, you will see "javascript library" on the left hand side. The "dynamic option list" may help you to do what you want to achieve.
If the information is relevant and you have the problem solved, please show me your code.
jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Hi, JiaPei:
Thanks for your info. I am not very familiar with JavaScript, and I believe my problem is a bit different from it. Actually, I have not reached this point. Lets work together on this, and I believe we will be able to do it with kind help from Junilu and other experts.
what I currently want to do is: find a way to send the results back to my jsp page. For example, if I have the following 2 lists:
[company list]
[department list]
[empoyee list]
If employee list is a dependent list of department list. Suppose we have JavaScript function-populate-to do the job for us, my first problems are:
-If I use this script function, do I stll have to
use action class to send empolyee list back?
-how to pass relevant employee list to my Jsp page
based on the selected item?
I am thinking about send the selected item to an action class to process and get results, but it seems the select action will not make action form to pass the selected department to action class, we need a submit and have to send all 3 items back (Am I understanding correctly?). So, this method may be wrong.
But if I create another action class for department selection, I will need another html:form, multiple forms are not allowed?
I just cannot think of a nice way to handle this.
Jim
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Hi, Jim: Someone (not I) suggested a way (see below) to approach the problem:
=================================================
You could have the JavaScript event onChange submit the information to a hidden iFrame that contains an Action that queries the database. Then have the iFrame populate the list dynamically with JavaScript. And this web site uses the method.
http://www.rvcountry.com/sales/inventory/Entry.jsp
=================================================
[ July 22, 2003: Message edited by: JiaPei Jen ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6


I am thinking about send the selected item to an action class to process and get results, but it seems the select action will not make action form to pass the selected department to action class, we need a submit and have to send all 3 items back (Am I understanding correctly?).

Somehow I get a sense that you are not quite clear on some of the basics, so I'll just go over the general flow of things quickly to make sure that we're on good footing:
1. JSPs are processed on the server side (the JSP source is translated into a Servlet, which runs on the server side). When the JSP is invoked (actually, the resulting Servlet is invoked), the result of the processing is a response (usually HTML), which is sent back to the client (usually a browser). The browser knows nothing about JSPs. All it is aware of is HTML and JavaScript.
2. JavaScript is processed on the client/browser side. The server does not process JavaScript code at all; to the server it is merely text it read from the JSP that goes directly into the response (HTML) it sends back to the client.
So how does Struts fit into this? Well, the heart of Struts is the ActionServlet. When the web container receives a request for a Struts URL (usually ending in .do), it hands off the processing of that request to the Struts ActionServlet. The ActionServlet figures out what the matching Action and ActionForm it needs to use based on information found in struts-config.xml (it matches the request URL with an action-mapping name). It then gets an instance of the ActionForm (one instance per session), calls the ActionForm.reset() method, then takes all parameters from the request and puts the values into the matching ActionForm fields. If the mapping is configured to do so, it will then call the ActionForm.validate() method. Then it will instantiate an Action and pass the ActionMapping, ActionForm, HttpServletRequest, and HttpServletResponse to the execute method.
In the execute method, you normally access any values that came in with the request through the ActionForm (which now contains all the form values that were submitted with the request). When you exit from the execute method, you return an ActionForward, which tells the ActionServlet to which resource (usually a JSP page) it should hand off processing to in order to generate a response. Before it relinquishes control though, the ActionServlet puts the current ActionMapping into the request context. This allows the Struts html tags to access the ActionForm that you are processing. The ActionServlet then relinquishes control to the web container, telling it what resource (as defined by the ActionForward) should take over. If that resource is a JSP, then the web container lets the Servlet/JSP engine/translator take over processing.
If you have any Struts html tags in your JSP, these tags will know to pull out the ActionMapping from the request, figure out what form to pull from the session (attribute name == ActionMapping.getName()), then get the current values of the corresponding form properties and render an appropriate html input tag with a value attribute.
So, if your ActionForm's getFirstName() method returns the String "Jim" and you have a custom tag in your JSP
<html:text property="firstName" />
this will be rendered in the response (what the browser sees) as
<input type="text" name="firstName" value="Jim" />
Whew! That's all there is to it (in general).
I know that all this may seem far from what you asked but it is important that you understand the context in which Struts operates and how it fits into the general scheme of things if you want to begin to understand Struts at all. (Remember, anything we don't understand is indistinguishable from magic--or something like that ;))
It's late now (or early morning, if you want to look at it that way) and I have to get some rest befor I have to go to work again in four hours so I'll try to get back to the original question later. Meanwhile, try to make sense of what I just wrote and try to get the flow straight in your mind.
jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Thanks, Junilu:
That was a wonderful explanation, it clears my missing links of the general flow. I did read a struts book, but I lost track of part of what you said once I put my book down.
Its certainly very relevant to my problem. I guess multiple forms are possible, the only problem now is how to make a selction something like a submit (that is, it will sends the selected parameter over to action class).
regards,
Jim
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Jim, you don't need multiple forms or Actions for this. As you may have already gathered, there are several ways to do this. The solution that does not involve JavaScript is, in my opinion, not very user-friendly although I would not want to go to the other extreme of having JavaScript dynamically update the employee list either. The compromise would be to use JavaScript to submit the form on the onchange event of each select element.

In the Action, check for a change in the selected company, department, employee (in that order) and retrieve the dependent list as appropriate. You'll have to decide on how you will store the old selected values though because the ActionForm will always contain the currently selected value. The most straightforward way would probably to have properties in the ActionForm for these as well, e.g. prevCompany, prevDepartment, prevEmployer and add a method, say saveSelected() and some convenience methods:

Now this is just off the top of my head and there may be a few other things you'll have to check to avoid getting errors but that's the general idea at least.
[ July 24, 2003: Message edited by: Junilu Lacar ]
jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Junilu, thank you very much for your new idea.
As you guessed, I have tried using two action classes, or using javascript code to dynamically populate dependent lists, both are not successful.
I am almost desperate on this thing.
The tricky part for this problem: I need to pre-populate company and department lists, and create
a empty employee list before user select department and populate employee list. So, I need my action class to do this first. Once this is defined, I cannot do other things in this action class, right?
Now, in order to auto-submit a selected department like what you pointed in your code above, I need to define another action class, put employee results into session scope for employee list to use. But if I use another form for this, the pre-population part will be broken, I don't know how to pre-populate the following forms (I always associate actions with forms, am I right on this?):
<html:form>
[company-list]
</html:form>
<html:form>
[department-list]
[employee-list]
</html:form>
If I use your above design, I face the same problem: how can I pre-populate two lists and init third blank one if I use my only action class to detect department selections? This is like a blackbox to me.
Sorry, I did not mention I did not need to populate
department as a dependent list. Once company and department lists are prepopulated, they will not change. Only employee list will be dependent on department list.
I have several questions about your design:
- Why do I need to store old values? If I select a new department, new employees will be filled in the empolyee list. User will select a company, a department, and an employee once employee list is filled, then use three selections to do a saerch and display results on the bottom of the page. So, I only need current selections. I may miss something since I am totally unexperienced to Struts.
-Like I asked above, how can I handle pre-population, and dependent population in one action class. (If I use 2 html:form's (actions), I cannot think a way to pre-populate lists either).
-If action receives the selected department, it gets employee list from db, I am still not sure how it will dynamically fill employee list in jsp page.
If I do sth like this:
session("employees", employees)
Will jsp page look for the results in sesion scope and dynamically fill employee list? This sounds like need another actionMapping to do the job?
regards,
Jim
[ July 24, 2003: Message edited by: jim yin ]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

I was working on the wrong assumption. I thought the flow would be: select a company, retrieve & display list of departments for the selected company (implies that each company may have a different set of departments), select a department, retrieve & display list of employees for the company/department.
If each company has the same set of departments then that simplifies things a little. You still wouldn't need to have multiple actions/forms though and you can use a simplified variation of the code I gave.
jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Originally posted by Junilu Lacar:
I was working on the wrong assumption. I thought the flow would be: select a company, retrieve & display list of departments for the selected company (implies that each company may have a different set of departments), select a department, retrieve & display list of employees for the company/department.
If each company has the same set of departments then that simplifies things a little. You still wouldn't need to have multiple actions/forms though and you can use a simplified variation of the code I gave.


Thanks, Junilu:
Could you help answer the above questions I raised?
I think they are the key for me to find a solution.
For example, how do I handle pre-population, retrive selected dept and employee list in the same action?
regards,
Jim
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

This is a very rough description so you'll have to fill in the meat, OK?
In struts-config:
form name="selectForm"
...
action mapping path="/select" name="selectForm"
forward name="success" path="select.jsp"

ActionForm:
define fields and getters/setters for:
- company
- department
- employee
- action
reset():
company=null;
department=null;
employee=null;
action=null;
Action:
public ActionForward execute(...) {
SelectForm selectForm = (SelectForm) form;
...
if (selectForm.getAction() == null)
    //initialize form
    //load collections for
    //company & department
    //set default selected values
    //for the form fields
    //if you want.
else
    //load list of employees for selected company+department
return mapping.findForward("success");
}
in JSP:
<html:hidden property="action" value="getemployees" />
<html:select property="company"
onchange="javascript:document.forms[0].submit();">
....

To bring up the page, make a link to "/select.do"
This should call the Action and make it do the initialization and pre-populating. Then when the user changes a selection for company or department, the request will come in with the form.action field set to "getemployees" which is the value of the hidden "action" field in the JSP.
The actual value you use is up to you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: First struts app help! Drop-down lists