aspose file tools*
The moose likes Struts and the fly likes how to populate a list in a another select tag using a select tag using struts 2 & DOJO (ajax) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to populate a list in a another select tag using a select tag using struts 2 & DOJO (ajax)" Watch "how to populate a list in a another select tag using a select tag using struts 2 & DOJO (ajax)" New topic
Author

how to populate a list in a another select tag using a select tag using struts 2 & DOJO (ajax)

vijay dadhwal
Ranch Hand

Joined: Dec 02, 2008
Posts: 47

Dear all,

i have two select tags in my jsp :-
(1) Select tag 1 -> which has prepopulated countries name
(2) Select tag 2 -> i want this to be populated with the city names, based on the selection of country in select tag 1

I donot want to use submit button as it will take me to another action.

Using DOJO (ajax) how could i call different action which populate the city select tag 2.

Please help ?
Regards
Vijay umar


MCA , SJCP
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

what exactly is your question? You obviously know you have to use ajax ...
vijay dadhwal
Ranch Hand

Joined: Dec 02, 2008
Posts: 47

Hi,

Thanks for the reply.

I solved it.

Regards
Vijay Kumar
raj jaiswal
Ranch Hand

Joined: Nov 25, 2008
Posts: 56
Hi
Vijay Kumar, I am raj jaiswal actually i am also facing same problem with Struts 1.3 and Ajax.If you share your knowledge that would be really helpful for me.



Thanks & Regards
Raj Jaiswal
vijay dadhwal
Ranch Hand

Joined: Dec 02, 2008
Posts: 47

Sure Raj,

Below is the jsp page which is using ajax

select-dojo.jsp


select2.jsp code


if that much info is allright then ok otherwise i can also share action classes code with you.

Thanks
Vijay
raj jaiswal
Ranch Hand

Joined: Nov 25, 2008
Posts: 56
hi vijay thanks very much for reply...
This is ok but currently i am using struts 1.3 so that i thing it will not help to me .


Anyway once again thanks .

Thanks
Rajjaiswal
vijay dadhwal
Ranch Hand

Joined: Dec 02, 2008
Posts: 47

Hi Raj,

for Struts 1.x,

you can use the below mentioned jsp page, here i am using pure xmlhttprequest object for ajax calls which is supported by all the browsers (might be differently)

select1.jsp



select2.jsp



Hope this would solve your p/bs.

Regards
vijay
kanishk dewan
Greenhorn

Joined: Aug 11, 2011
Posts: 9
Hi Vijay,

I know I am raising up this thread again after such a long time, but after hours of search this is where I think I might find my solution. Please excuse me if you find this post lengthy.

I am using the same method as you used for updating drop down lists (struts 2 version). But the problem I face is that although my action class gets executed (I set the lists for the drop downs in action class), the drop downs don't get populated according to the lists set in the action class.

Also I would like to clarify 3 things:

1.) <s:url action="select2" var="url"/>
Here, did you use "select2" in action because your action class name is "select2.java" or because your jsp page name is "select2.jsp" ?

2.) list="#application.countries"
Here, I assume that the list name that you set in the action class was "countries". Then why did you use "#application.countries" instead of simply "countries".

3.) In my case I have 5 drop downs that have to be updated in case any one of then is changed. So the way you have done it, do I have to create a separate jsp for each drop down ?

Here is what I have done, please have a look:

My jsp page:




Here is my struts config (although I dont think there is any issue here):




Please help me in resolving this issue asap. Its really bugging me.

Thanks!!!
vijay dadhwal
Ranch Hand

Joined: Dec 02, 2008
Posts: 47

Hi kanishk,

No Bothers,


Answers to your queries

1.) <s:url action="select2" var="url"/>
Here, did you use "select2" in action because your action class name is "select2.java" or because your jsp page name is "select2.jsp" ?

Ans: select2 is the name of the action as reffered in struts.xml file.


2.) list="#application.countries"
Here, I assume that the list name that you set in the action class was "countries". Then why did you use "#application.countries" instead of simply "countries".

Ans: In Struts 2.x, All the Lists, Maps & Arrays are stored under Context Map in Struts Object Stack not under value stack, so to refer to any object stored under context map you always prefix it with # keyword and also the "countries" object is stored in Application Context hence i have prefixed it as application.countries
hence it is [context map->application context->object] i.e. "#application.countries

3.) In my case I have 5 drop downs that have to be updated in case any one of then is changed. So the way you have done it, do I have to create a separate jsp for each drop down ?

Ans: I actually was using same jsp file earlier but i noticed in IE8 that whenever i was selecting the countries select option, it was stuck on a loop, since the same action was called in ajax and result was forwarded to same jsp. Hence i choose different action and different jsp page.

But this can be cracked i think.
(1) Assume select.jsp has both the select boxes (countries & cities).
(2) when countries select box option is selected, call an action in ajax, the action would populate the cities select box.
(3) This action should not forward to any jsp page but only should set the cities object based on the countries selection.

please read about <action> tag of struts which has option of avoiding forward / redirect to any page after an action is triggered.

I could help you in this but i really want if you attempt it yourself atleast once.

If any doubt, please write back.

Regard
Vijay












kanishk dewan
Greenhorn

Joined: Aug 11, 2011
Posts: 9
Hi Vijay,

Thanks for such a quick reply.
I am still not able to work it out even after a lot of tinkering around, taking inputs from your posts.
Here are some findings:


1.) In the following tag

as soon as I change "<sx:a>" to "<sx:div>" I have a strange error:

"Error loading '/getLists.htm' (404 Not Found)"

is displayed on the top of the page, but the page is displayed and functions correctly, i.e. the action is called and executed correctly, except for one thing: the function that was supposed to execute only when the onchange event occurs, also executes when the page loads for the first time, I guess that could be the reason that was causing the looping problem on your page.


2.) The 2nd thing is that, when I use "#application.customerList" instead of simply "customerLists" I get the following server error :

The requested list key '#application.customerList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location].

In this I would like to point out that when (using simply "customerLists") I print the lists in the action class (on the server console) they get printed as:
[contname1, contname2]
[custname1, custname2]

i.e. in the form of values instead of objects. I guess this is fine, but was just giving additional info, in case it means something.


3.) I looked into struts 2 action tag on apache struts 2 documentation site, and I think I found a way. I will see if it works once I get to that stage, i.e. once I encounter the "looping problem".

Because till now all thats happening is that the action gets executed correctly but it doesnt effect the lists of the drop down.


Please advice.

Thanks!!
kanishk dewan
Greenhorn

Joined: Aug 11, 2011
Posts: 9
Hi Vijay,

I have not been able to get around this till now . Waiting for your reply.

Please help asap.

Thanks!!

kanishk dewan
Greenhorn

Joined: Aug 11, 2011
Posts: 9
Hi Vijay,

Now I am facing the looping problem.
As you said I tried modifying so that the action does not forward to the jsp page.
I tried removing the result tag in the action tag in struts config file, so that the action does not forward to the page.
But doing so causes the drop downs not to be populated.
Was there some other method you were referring to for not allowing page forwards?

Waiting for your reply.

Thanks!!
Aatka Ali
Greenhorn

Joined: Aug 24, 2011
Posts: 4
Hi All....!

Same problem i am facing i am new to java i am not using strust.. i am trying to create Ajax based application that using a Jsf and a Jsf page calling a servlet...the servlet is showing the dropdown list that displays the name of the user in form of the list as i typed A in the text box of my page the All user name starting with alpabet 'A' must come in the drop down list ...

Code i am sending is working but the drop down list is not displaying

i am using a JSF page home.jsp




my servlet is HintSevlet.Java

kanishk dewan
Greenhorn

Joined: Aug 11, 2011
Posts: 9
Hi Aatka Ali,

This thread is not related to your problem. What you need is an Autocompleter (a dojo component) which allows user to see the drop down contents change as he/she types in the textbox. Google it you should find lots of help.

Also I think you should use Struts framework, unless there is some technological constraint. Try it. Its not very difficult.

Regards
Kanishk
poornimaa ananth
Greenhorn

Joined: Mar 23, 2012
Posts: 3
Hi Vijay,

I Am working on struts 2 to be deployed on IBM portal server. Need to use ajax. when a textfield is populated an action has to be invoked. based on the result a dropdown has to be enabled or disabled. I have tried the other samples provided by you but with no result. Need help asap. Thanks in advance.
Sanyyam Sharma
Greenhorn

Joined: Aug 02, 2011
Posts: 2
Hi Vijay,

Can you please provide a sample code for the same.

JSP, ACTION for same.

Best regards,
Sam
 
jQuery in Action, 2nd edition
 
subject: how to populate a list in a another select tag using a select tag using struts 2 & DOJO (ajax)