File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes iterate doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply locked New topic
Author

iterate doubt

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I have an iterator which should iterate an ArrayList present in a session as...

<s:iterator value="#session.cityList" status="element">
<option><s:property value="???"/></option>
</s:iterator>

cityList is an ArrayList containing a few city names...

what should be the 'value' of "s:property" in the above to display all cities ??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If it's an array of strings, you don't need to have *anything*--the property tag will display the top-most value stack value by default. If the cities are an object, the property tag's value attribute should be the name of the city property you want to display.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
It is an Arrayist of objects like...

say Australia,
sydney,hobart,Adelaide...

so now what would property take as I have put hem in a session and iterate?

If the cities are an object, the property tag's value attribute should be the name of the city property you want to display.


like this?



this doesn't work !!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Pradeep Adibatla wrote:It is an Arrayist of objects like... say Australia,sydney,hobart,Adelaide...

That tells me nothing about what kind of objects they are. Are they strings or not?
like this?
this doesn't work !!!

Correct, it doesn't, because you're using random expressions to try to get it to work, instead of doing what I told you to.

If the objects in the list are strings you don't *need* a "value" attribute in your property tag. If they're objects *containing* a string with the city name, then the "value" attribute should be the name of the object property containing the city's name.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
oops actully yes...
I declared the ArrayList like this


and ...



rather i think it should be...



and then it should be...



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's a lot of typos for such a small chunk of code :( Be careful! :)
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
A small correction...

I had to use a hashmap but the problem persists somehow... I think i could manage with this without converting into ArrayList...

in action...




in jsp...



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Each object in a Map iteration is a MapEntry.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Sorry, I couldn't comprehend that !!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you're using <s:iterator> over a list, each object will be whatever class is in the list.

If you're using <s:iterator> over a Map, each object will be a Map.Entry.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I thought this should be fine...




Even this couldn't help...




What else could be?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why get the entrySet? You're already iterating over a map, no?

So how is the map being filled? It'd be a *lot* quicker if you'd post everything related. It's clearly *something* you're doing wrong, and I'd rather just get it all dealt with at once rather than drag this out over days.
Lucas Grijander
Greenhorn

Joined: Feb 10, 2009
Posts: 9
first you have to create a List with all the cities you want to be displayed

second, in your jsp use just <s:iterator list="yourList"/>

this only work if the objects of your list are Strings
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please make sure that posts help solve the problem--we're already past that point, not dealing with a list, not dealing with simple objects, and so on.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336

Even this is a error....

actually in the jsp there is a textfield and a button (OK) next to it followed by select box...

The user enters the Country name and click 'ok' button next to it.

Then I do an Ajax call sending the country name from javascript to action class...

Now on recieving the country name I get all the cities in that country using my 'City' class ( the code I wrote earlier) and I am populating in the select box...

Hope it's clear.Is it a wrong process to populate like that? kindly explain...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You mean you're trying to populate a field on the page you're on by making an Ajax request?

If so, that most certainly won't work--the entire page you're on is rendered before anything else happens, that's just how web applications and JSP work--everything in the JSP happens on the server side. If you want to make an Ajax request and populate a field on the current page then you'd need to write the JavaScript to actually *do* that.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Hey ! Thanks David.Now I know the reason...

The reason why I am making an Ajax request is to get the cities of the country selected! If there are alternative ways then I could be extremely happy to know!

If you want to make an Ajax request and populate a field on the current page then you'd need to write the JavaScript to actually *do* that.


What should be my next step.What should the javascript contain and what will be the way to populate it?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Moving to HTML/JavaScript.

How to do it depends on the JavaScript framework you're using.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I havn't used any framework .It's normal javascript.If somethng like Jquery or Dojo gives comfort,I would jump on them but I am desperate to finish this!!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

Ajax is too big a subject to cover in a forum post. Heck, entire books are written on the subject. I suggest grabbing such a book or finding an online tutorial.

This article might be a good place to start.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It'll be quicker to spend the 10-15 minutes necessary researching how to do this in a JavaScript library than to try to write and debug it by hand in a way that works across browsers.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Yeah that how to do that part is void for me here.I am stuck...now what sould be done in JS?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

Bear Bibeault wrote:I suggest grabbing such a book or finding an online tutorial.

This article might be a good place to start.


If you want some code example, download the code for my jQuery in Action book and look in the code examples for chapter 8. The code, of course, assumes that you will use jQuery.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
ok! I have you book "Ajax in Action" and I have been trying to do this using Json...

I added json 0.32plugin jar...

added the mapping in struts.xml

I tried to convert that into Json array...





I couldn't proceed further! Kindly provide some direction... I will work over your suggestion.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(That's actually a different book.)

So now you need to take that JSON object, evaluate it in JavaScript, and use it to populate the select control.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
So now you need to take that JSON object, evaluate it in JavaScript, and use it to populate the select control.


I have searched for so many hours just couldn't progress anywhere?

Kindly tell me the way forward in terms of implementation....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Five seconds on Google:

http://www.keyframesandcode.com/resources/javascript/jQuery/demos/populate-demo.html
http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

I gave you a direct link to sample code. How much simpler can we make it?
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Thanks, David and Bear for those inputs.


Like in this example...




Looks superb,you could see a "select.php" there.

I was wondering how it would be in struts2 after having prepared the json object as shown in my code.I was talking about an example with struts2 which I thought I couldn't find out beacause jquery and php(atleast for the example) are unknown to me so still coming to terms with them .

I think i would need to change that "select.php" to "cityAction.action"

Now if I try to modify this according to my one,I could do this...



But I don't know what the above one does...

I am fighting it out and will continue till I do so !!!

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
After the Ajax call is made on selecting the country,I passed the country.Now,I have got this in my Action class...





and this in jsp...




how should I proceed now?? what are the corrections?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What doesn't work? ItDoesntWorkIsUseless.
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I added this for json in struts.xml...




I also have the normal one...




Currently I am in cules.jsp...

I have a button 'tab' which takes me to tabtest.jsp which contains the 2 select boxes...



Now the problem is before I go to tabtest.jsp to select a country,when i click the button 'tab'(as said above) I get a window and I have no clue about this....

----------------------------------------------------------------------
opening rules.action

You have choosen to open rules.action

firefox is allowing you to open or save this file [Browse]
.......

OK Cancel buttons
---------------------------------------------------------------------

instead of being directed to tabtest.jsp as it was before adding that json mapping in struts.xml

I get this one...
-------------------------------------------
Current policy properties
thread.thread_num_limited: true
file.write.state: disabled
net.connect_ex_dom_list:
mmc.sess_cab_act.block_unsigned: false
mmc.sess_cab_act.action: validate
mmc.sess_pe_act.block_blacklisted: false
mmc.sess_pe_act.block_invalid: true
jscan.sess_applet_act.stub_out_blocked_applet: true
file.destructive.in_list:
jscan.sess_applet_act.block_all: false
file.write.in_list:
file.nondestructive.in_list:
window.num_limited: true
file.read.state: disabled
jscan.session.origin_uri: http://jsonplugin.googlecode.com/files/jsonplugin-0.32.jar
file.nondestructive.state: disabled
jscan.session.user_ipaddr: 10.81.82.131
net.connect_other: false
thread.thread_num_max: 8
file.destructive.ex_list:
file.nondestructive.ex_list:
file.write.ex_list:
jscan.sess_applet_act.sig_invalid: block
file.read.in_list:
mmc.sess_cab_act.block_invalid: true
jscan.session.policyname: TU1DIERlZmF1bHQgUG9saWN5
mmc.sess_pe_act.action: validate
thread.threadgroup_create: false
net.connect_in_dom_list:
net.bind_enable: false
jscan.sess_applet_act.sig_trusted: pass
jscan.session.user_name: 10.81.82.131
jscan.session.user_hostname:
file.read.ex_list:
jscan.sess_applet_act.sig_blacklisted: block
jscan.session.daemon_protocol: http
net.connect_src: true
jscan.sess_applet_act.unsigned: instrument
mmc.sess_pe_act.block_unsigned: false
file.destructive.state: disabled
mmc.sess_cab_act.block_blacklisted: true
window.num_max: 5


what could be it? If I remove that json mapping,I dont get this .Anything wrong with the mapping?I can't make it a single mapping,can I?



Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Guys,Is the information insufficient? Experts kindly check!!!

This is my form...




Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

<s:form ... is not valid HTML. If this is a Struts question, this is not the place to ask it. If it's an HTML question, don't show Struts markup here.>
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Bear,please move it to struts forum...I initially posted this is struts forum...

May be David might take a look...thanks!!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please start a new thread; this has gone back and forth already.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: iterate doubt