1

I am trying to build a web application using Struts 2 and jQuery.

On change of a dropdown list, I need to fetch the details from the database. In my struts.xml configuration, I have defined the method and action as follows:

<result name="addressChange">
    <param name="root">requestBean</param>
</result>

When I executed the application after making a change in all the respective places, the request was passed on to the Action class and DAO methods and the control is returned to screen successfully. But the data returned was not available on the screen and I was getting an error message that:

the url ... 404 not found (anonymous function) ajax in jquery.

Since I had some other ajax call, I compared them and found out that the result type is missing in the struts.xml and then I changed it likewise. Its working fine now.

<result name="addressForBrigade" type="json">
    <param name="root">requestBean</param>
</result>

Questions are:

  1. How come Struts identify it as JSON object and how does it cover it to the POJO?
  2. When should we use the result type as JSON in Struts 2?
  3. Can we use other result types?
  4. Should we need to use the result type as JSON whenever there is an AJAX call?
Roman C
  • 49,761
  • 33
  • 66
  • 176
Anand
  • 727
  • 3
  • 14
  • 39
  • 1
    I've already [linked this](http://stackoverflow.com/questions/17093862/jquery-ajax-issue-returning-json-value/17149414#17149414) when answering your other question; in that Q&A there is an answer from RomanC explaining how to use the `stream` result, if you want to return a JSON object without using a `json` result. BTW JSON is the standard for AJAX call nowadays – Andrea Ligios Sep 16 '14 at 09:46
  • @AndreaLigios: Thanks for your suggestions. It gives a clear idea now. My biggest challenge is, when I enter values in the text box and hit ENTER key, the form gets submitted, I mean the respective action method gets called. I am trying to not submit the form, but could not. I used preventDefault() to stop and some other approaches, but couldnot. Can you please help me with this too? – Anand Sep 17 '14 at 07:31

1 Answers1

1

First question seems you have answered yourself, when you added type="json" to the result. Without it the "dispatcher" type is used implicitly. You should use json result type if you want to return JSON object with a response. You can use other result types, it depends what do you want to return with the response.

It makes sense to specify Content-Type header when formatting your data to the response. You don't need to specify json result type to results that don't return JSON object, use other result types, but you should use dataType property in jQuery Ajax to specify a data type you're expecting back from the server.

Each result type is defined by the configuration and picked up by the dispatcher when it's executed. If you didn't specify the result type, the "dispatcher" result type is used which invokes a request dispatcher to forward to the JSP for rendering it's output to the response. It merely produces a HTML content that you can see in the browser source window.

Roman C
  • 49,761
  • 33
  • 66
  • 176