tags:

views:

47

answers:

1

So, I am getting as return parameter from an already established code a StringBuilder element, and I need to insert it into my GWT app. This StringBuilder element has been formatted into a table before returning.

For more clarity, below is the code of how StringBUilder is being generated and what is returned.

private static String formatStringArray(String header, String[] array, int[] removeCols) {
    StringBuilder buf = new StringBuilder("<table bgcolor=\"DDDDDD\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">");

    if (removeCols != null)
        Arrays.sort(removeCols);

    if (header != null) {
        buf.append("<tr bgcolor=\"99AACC\">");
        String[] tokens = header.split(",");
        //StringTokenizer tokenized = new StringTokenizer(header, ",");
        //while (tokenized.hasMoreElements()) {
        for (int i = 0; i < tokens.length; i++) {
            if (removeCols == null || Arrays.binarySearch(removeCols, i) < 0) {
                buf.append("<th>");
                buf.append(tokens[i]);
                buf.append("</th>");
            }
        }
        buf.append("</tr>");
    }

    if (array.length > 0) {
        for (String element : array) {
            buf.append("<tr>");
            String[] tokens = element.split(",");
            if (tokens.length > 1) {
                for (int i = 0; i < tokens.length; i++) {
                    if (removeCols == null || Arrays.binarySearch(removeCols, i) < 0) {
                        buf.append("<td>");
                        buf.append(tokens[i]);
                        buf.append("</td>");
                    }
                }
            } else {
                // Let any non tokenized row get through
                buf.append("<td>");
                buf.append(element);
                buf.append("</td>");
            }
            buf.append("</tr>");
        }
    } else {
        buf.append("<tr><td>No results returned</td></tr>");
    }

    buf.append("</table>");
    return buf.toString();
}

So, above returned buf.toString(); is to be received in a GWT class, added to a panel and displayed... Now the question is: how to make all this happen?

I'm absolutely clueless as I'm a newbie and would be very thankful for any help.

Regards,
Chirayu

+1  A: 

Could you be more specific, Chirayu? The "already established code" (is that a serlvet? Does it run on server side or client side?) that supposedly returns a StringBuilder, obviously returns a String, which can be easily transferred via GWT-RPC, JSON, etc.
But like Eyal mentioned, "you are doing it wrong" - you are generating HTML code by hand, which is additional work, leads to security holes (XSS, etc) and is more error-prone. The correct way would be:

  • Instead of generating the view/HTML code on the server (I'm assuming the above code is executed on the server), you just fetch the relevant data - via any transport that is available in GWT
  • On the client, put the data from the server in some nice Widgets. If you prefer to work with HTML directly, check out UiBinder. Otherwise, the old widgets, composites, etc way is ok too.

This way, you'll minimize the data sent between the client and the server and get better separation (to take it further, check out MVP). Plus, less load on the server - win-win.

And to stop being a newbie, RTFM - it's all there. Notice that all the links I've provided here lead to the official docs :)

Igor Klimer
"And to stop being a newbie" you will do pretty well as a psychotherapist :D [pun intended]
Ashwin Prabhu
Thanks, Ashwin, it's always good to have a backup plan ;) I don't mind helping with even the simplest problems, unless I see that the person asking did not do his/her homework (jeez, I sound like a teacher). On the other hand, I'm kind of jealous of Chirayu, because he gets to play around with GWT for his internship :D (although I understand that getting shoved into the middle of GWT can be confusing and frustrating - you want to write a simple app, and suddenly you should know about MVP, UiBinder, different transports, security implications, optimization tricks, etc...)
Igor Klimer
@Ashwin, I wish I could crack a nasty pun at You, but sadly I'm a newbie(READ : ASS), and You are an expert.@Igor, I have not done my homework, but that was because of a time-constraint, I',m a C/C++ developer, thrown into java development environment at internship, with already existing code, for one part of which I'm integerating GWT. Sadly I'm stuck, have 7 days left, trying to wrap up things, and confused.
Chirayu
Chirayu
I understand your situation, Chirayu, that's why I'm providing answers and comments here. Actually, I also came from C++ to GWT :) And I've already answered your question above - without any specifics about your project (what does it use to communicate between server-client? Does it use UiBinder? Which GWT version?), I can't provide any source (and I'm rather reluctant to do so - all the code samples are in the docs or in the GWT samples). But do write if something is unclear :)
Igor Klimer
@Igor, thanks as always for your insight....I guess some battles we have to fight on our own in totality. Cheerz!
Chirayu