Simply forward to a JSP and render your output there, same as any Struts
Action.  It can be XML, JSON, HTML, some custom format, whatever.  I wish
I had written that article showing that in the first place since this is a
question that gets asked all the time, but I thought this was simpler (one
less bounce to follow the ball through), but in retrospect.. :(

Frank

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM/Yahoo: fzammetti
MSN: [EMAIL PROTECTED]
Author of "Practical Ajax Projects With Java Technology"
 (2006, Apress, ISBN 1-59059-695-1)
and "JavaScript, DOM Scripting and Ajax Projects"
 (2007, Apress, ISBN 1-59059-816-4)
Java Web Parts - http://javawebparts.sourceforge.net
 Supplying the wheel, so you don't have to reinvent it!

On Tue, September 11, 2007 11:46 am, aarthy wrote:
>
> Hi,
>
> In the below code,how can I avoid the HTML coding in the action
> class?(http://www.omnytex.com/articles/xhrstruts/xhrstruts.zip)
> I tried many different codings,but everything failed.
>
> public class Example3GetCharactersAction extends Action {
>
>
>   public ActionForward execute(ActionMapping mapping, ActionForm inForm,
> HttpServletRequest request, HttpServletResponse response) throws Exception
> {
>
>     // Get a list of characters associated with the select TV show
>     String tvShow = (String)request.getParameter("tvShow");
>     if (tvShow == null) {
>       tvShow = "";
>     }
>     ArrayList characters = getCharacters(tvShow);
>
>     // And yes, I know creating HTML in an Action is generally very bad
> form,
>     // but I wanted to keep this exampel simple.
>     String html = "<select name=\"CharactersSelect\">";
>     int i = 0;
>     for (Iterator it = characters.iterator(); it.hasNext();) {
>       String name = (String)it.next();
>       i++;
>       html += "<option value=\"" + i + "\">" + name + "</option>";
>     }
>     html += "</select>";
>
>     // Write the HTML to response
>     response.setContentType("text/html");
>     PrintWriter out = response.getWriter();
>     out.println(html);
>     out.flush();
>
>     return null; // Not forwarding to anywhere, response is fully-cooked
>
>   } // End execute()
>
>
>   // This method returns a list of characters for a given TV show.  If no
> TV
>   // show is selected, i.e., initial page view, am empty ArrayList is
> returned.
>    private ArrayList getCharacters (String tvShow) {
>
>                ArrayList al = new ArrayList();
>
>                        if (tvShow.equalsIgnoreCase("StarTrekTNG")) {
>       al.add("Jean Luc Picard");
>       al.add("William T. Riker");
>       al.add("Data");
>       al.add("Deanna Troi");
>       al.add("Geordi LaForge");
>     }
>
>     if (tvShow.equalsIgnoreCase("Babylon5")) {
>       al.add("John Sheridan");
>       al.add("Delenn");
>       al.add("Londo Mollari");
>       al.add("Stephen Franklin");
>       al.add("Vir Cotto");
>     }
>
>     if (tvShow.equalsIgnoreCase("StargateSG1")) {
>       al.add("Samantha Carter");
>       al.add("Jack O'Neil");
>       al.add("Teal'c");
>       al.add("Daniel Jackson");
>       al.add("Baal");
>     }
>
>                       return al;
>
>         } // End getCharacters()
>
> Thanks.
>
> --
> View this message in context:
> http://www.nabble.com/Ajax-using-XMLHttpRequest-and-Struts-tf4423481.html#a12617736
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


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

Reply via email to