On Nov 7, 2007 7:54 PM, Lalchandra Rampersaud
<[EMAIL PROTECTED]> wrote:
> I  would like to integrate ajax within a struts app, but I have no idea how
> to go about doing that.

The key point is that when a script makes an "Ajax request" (XHR), the
server doesn't know it came from a script, and handles it like any
other response. One reason Ajax is so successful is that it works just
fine with existing server technologies, including Struts.

It's not the Ajax request that is different, but the Ajax response.
Instead of returning an entire page for the browser to display (or
redisplay), an Ajax response will just return a portion of a page. The
response can take the form of XML, or HTML, or plain text, another
script, or whatever else the calling script may want.

Both Struts 1 and Struts 2 can return any type of response. We are not
limited to forwarding to a server page. In Struts 1, you can just do
something like

{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("Hello World!  This is an AJAX response from a Struts Action.");
  out.flush();
  return null;
}

In Struts 2, we can do the same thing with a Stream result.

package actions;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import com.opensymphony.xwork2.ActionSupport;public class TextResult
extends ActionSupport  {
  private InputStream inputStream;
  public InputStream getInputStream() {
    return inputStream;
   }
  public String execute() throws Exception {
    inputStream = new StringBufferInputStream(
      "Hello World! This is a text string response from a Struts 2 Action.");   
    return SUCCESS;
  }

<action name="text-result" class="actions.TextResult">
  <result type="stream">
     <param name="contentType">text/html</param>
     <param name="inputName">inputStream</param>
   </result>
</action>

On the client side, as Frank indicated, there are two basic
strategies, which can be mixed and matched.

First, you can use some type of JSP tag, like the AjaxParts Taglib.
Here, you don't have to know very much at all about Ajax or
JavaScript. The taglib does all the work, and you just have to figure
out how to use the taglib. :)

Alternatively, you can use a plain-old Ajax widget on a plain-old HTML
page. Here, the sky's the limit, but you have to actually learn
something about JavaScript as a language.

If it helps, my slides form Ajax Experience are on up at Struts University.

 * http://www.StrutsUniversity.org/

The "Retrofitting" presentation covers using taglibs, and the "Coding"
presentation covers using Ajax widgets.

HTH, Ted
http://husted.com/ted/blog/

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

Reply via email to