struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeromy Evans <>
Subject Re: AjaxTag 1.3 support?
Date Sun, 03 Feb 2008 00:15:24 GMT
I completely agree with Dave so I'll take a different tact. 

Based on this example:, 
I would also take the approach of returning a JSON Result via the JSON 
Plugin.  Only the configuration changes for an XML result.

So, my form looks like this:

<s:url id="json" value="/JSONList.action" />
<s:autocompleter theme="ajax" href="%{json}" dropdownHeight="180"/>

So, my struts.xml contains the corresponding action definition with a 
JSON result:

    <package name="example" extends="json-default">
       <action name="JSONList" class="package.ListAction">
           <result name="success" type="json">
              <param name="root">list</param>

And my action method looks like this:

private ACEntry[] list;

public String execute() {
    list = populateList()
    return "success";

public ACEntry[] getList() {
   return list;

In summary, the autocompleter does a get to /JSONList.action
Struts2 executes the execute method of ListAction
The execute method creates a structure that will populate the autocompleter
The action returns a JSON result type
The JSON plugin converts the root object (the list property only rather 
than the whole action) to a JSON structure
The JSON plugin returns the application/json content to the autocompleter

I'll leave it to you to determine what the list structure should like 
like.  As it'll probably be re-use I'd create a bean for the purpose. 
Use the FireBug plugin for firefox to inspect the format of the JSON 
result to ensure it matches the format required by the javascript 
library (or even better, write a unit test).
This will work with any javascript library that accepts JSON - you just 
have to provide an appropriate structure.  It will also work with any 
library that requires an XML response, except you'd use an XML result 
type like I previously described in this thread.

I hope that helps.  Struts2 is really well suited at providing non-html 
responses if you take the time to understand the ResultType architecture.

 Jeromy Evans

Dave Newton wrote:
> --- "Griffith, Michael *" <> wrote:
>> Thanks for the comprehensive reply. Looking at the documentation and the
>> example app, I am still not sure how to configure my action/response.  I
>> am using tiles 2.
> As previously stated if you are writing directly to the response you need to
> return null from the action. I am still not sure why you're using a tile
> definition here.
>> It seems I need to return my result in JSON format correct? By returning
>> a JSON String from the action, how to I configure my response?
> As Jeremy said this is, however, probably a bad idea, considering that the
> same outcome can be achieved in several different ways.
> Using a JSON result will serialize your action to JSON automagically. You'd
> use a "json" result (via the JSON plugin), not a tile. Or you could build the
> JSON manually and configure a FreeMarker result.
>> If I use the tiles response type, I get a dojo parsing error because the
>> whole page is returned from the reply. 
> Sure.
>> The example in the example app isn't very clear to me, as the action
>> returns a .JS file with the result hard coded, and not using any
>> template like tiles I might add.
> What example are you referring to? 
> When making an Ajax request for a component like <s:autocompleter.../> it's
> unlikely you'd want to return anything other than JSON. (Other components or
> component libraries may want other formats like XML or whatever.) Unless your
> tile definition is creating JSON it's not going to work, and it's unlikely
> that would be the best way to generate the JSON anyway.
>> Also, I think I found a bug? -- the namespace attribute seems to be
>> ignored when I pass href=%{search} to the autocompleter. I had to put
>> qualify the namespace in the value to get the correct action to be
>> invoked.
> When you configure the package does your namespace have a leading "/" (slash)
> character? If it doesn't this can lead to dispatching issues.
> Dave
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message