Neil Meyer wrote:
Hi All,

This ons is off topic again.
When I loop for an arraylist I normally use the the first option as it seems
to me that it will execute the quickest. I know supposedly I should use the
foreach loop.
All that I want to do is go through the list no adding new elements or
anything.

Am I correct to say that the first option is best in this case?

for (int i = 0; i < a.size(); i++) {
    System.out.println(a.get(i));
}

The above is very slighly faster for an ArrayList object but the code only works if a is a list.


for (Iterator<String> iter = a.iterator(); iter.hasNext();) {
    System.out.println(iter.next());
}

In JDK 1.5 at least, the preferred way to write the above is:

for (String s : a) {
     System.out.println(s);
}

It is very clean. Writing the loop this way has the least points of (human) failure. It works for any a as long it is a Collection objection. (And your own custom objects as long as they implement java.util.Iterable.)

HTH,

Jonathan Revusky
--
lead developer, FreeMarker project, http://freemarker.org/



Regards
Neil Meyer

-----Original Message-----
From: Frank W. Zammetti [mailto:[EMAIL PROTECTED] Sent: 14 March 2006 05:31 PM
To: Struts Users Mailing List
Cc: user@struts.apache.org
Subject: Re: [OT]Struts - AJAX, best solution

On Tue, March 14, 2006 9:32 am, Ashish Kulkarni said:

Hi
I have a couple of questions below
1 What is the best soluction to have struts and AJAX
work together?
I have read about DWR, Java Web parts, AjaxAnywhere,
But which is the good one, and why?


Well, of course I'm going to say AjaxTags in Java Web Parts :)  I think it
is unique among AJAX solutions at this time because it is almost entirely
declarative.  There is (in most cases) zero client-side code to write, yet
it also offers a great deal of extensibility to make it even more
powerful.  This makes it, I think, attractive to many people, those that
don't quite have the client-side skill but have a ton of server-side
skill.  It can in essence grow with your Javascript skills.

However, I will say that I recently used DWR for a project and I totally
love it!  It's very clean, very easy (*IF* you have some Javascript
knowledge) and really, in my experience, works very well.  It also offers
some ready-made integration with popular libraries like Struts, Spring and
Hibernate.  I have no problem at all recommending DWR (I liked it so much
that I'm hoping at some point I can contribute to it).

(FYI, because of the extensibility AjaxTags in JWP offers, I'm thinking of
writing a handler to integrate with DWR, so you'll be able to use it, to a
limited degree at least, in the same declarative fashion).

Dojo also gets a lot of rave reviews, as does Scriptaculous.  I think it
all depends on what your looking to accomplish... DWR and JWP are a bit
more low-level than some of the others... for instance, they don't offer
widgets and such.  If your after some of the more high-level things like
widgets and special effects and such, Dojo and Scriptaculous are
definitely worth exploring.


2 What is better for response XML file or comman
delimeted string, or build HTML in action class and
pass it to java script to replace it.


Very much depends on what your doing.  I will say that contrary to the X
in AJAX, my experience has been that people tend to NOT use XML at all. XML parsing on the client is a somewhat expensive operation, so certainly
if your returning more than a small chunk of XML, you might want to
consider if XML is the best choice.  In the end though, it depends on what
your returning.

(FYI: I'm not sure this is common knowledge, but I wrote a client-side
implenentation of Commons Digester, which can be found in Java Web Parts. It doesn't offer the full Digester capabilities of course, but if you like
Digester for XML parsing, as I do, you may want to have a look).


3 Also if i have my own java script to do Ajax
what do i return in Action class, normally in action
class i do
mapping.findForward("success"); after loading the
form, this will redirect response to the required JSP.
How does this change in AJAX, how do i reload only
part of JSP.


You can do one of two things... first, you can render the entire response
in your Action, and then return null.  This tells Struts that teh response
is fully formed and no forward/redirect is required.

Alternatively, and again I'm not so sure this is common knowledge, you can
simply forward to a JSP like always and allow IT to render the AJAX
response.  For instance, if your rendering some XML, how easy is that to
do with JSTL and Struts tags?  Pretty easy!  No need to write a bunch of
out.println's in an Action, just use a JSP!  The client doesn't know how
the response was generated, it just takes the response as-is.

Hope that helps!

Frank



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

  • Re: [OT] Jonathan Revusky

Reply via email to