Search code examples
javagwt

How to insert a StringBuilder element into a GWT app?


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


Solution

  • 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 :)