4

I call a Struts2 action using jQuery Ajax like the following:

 $.ajax ({  
        url: 'callAction.action',
        type: 'POST',
        data: data,
        dataType: 'string',
        success: function (data) {
           console.log("Success");
        }
});

And in response, it has to return a string back to jQuery.

private String result;
//getters and setters

public String call()
{
   //some code
   result= "some string";

   return SUCCESS;
}

I want to retrieve the result from the function in the Struts action to jQuery. How would I make this possible?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Gsm
  • 150
  • 1
  • 2
  • 9
  • The advantage of JSP vs Ajax is you don't call a server. Following the paradigm don't call us, we call you. – Roman C Jan 29 '15 at 21:43
  • @RomanC Could you explain it further? – Gsm Jan 29 '15 at 22:16
  • May be if you ask a good question, not like this. – Roman C Jan 29 '15 at 22:26
  • 3
    I did not understand your first statement. Would be great if you could clarify me on that. – Gsm Jan 29 '15 at 22:44
  • A good question should have a clear problem statement and include a relevant code demonstrating it. Your question doesn't fit these conditions. Please add details and description of the problem you experience and a short path to reproduce it. – Roman C Jan 30 '15 at 09:55
  • Definitely not a duplicate `JSON != String`. – Aleksandr M Jan 30 '15 at 15:41

1 Answers1

2

You can use stream result to get just a String from the action.

Configure your action to use stream result with contentType set to text/plain (or don't use contentType at all, because text/plain is set by default).

<action name="callAction" method="call">
    <result type="stream">
        <param name="contentType">text/plain</param>
    </result>
</action>

In your action create InputStream field with getter/setter and in your action method convert String to the input stream.

private InputStream inputStream;
// getter/setter

public String callAction() {
    inputStream = new ByteArrayInputStream(
            "some string".getBytes(StandardCharsets.UTF_8));
    return SUCCESS;
}

Then you can execute ajax request like that:

$.ajax ({  
    url: '<s:url action="callAction"/>',
    type: 'POST',
    dataType: 'text',
    success: function (data) {
        console.log(data);
    }
});

Note: it is better to use <s:url> tag to construct url-s and there isn't such dataType as string, use text or don't set it at all (jQuery will try to infer it based on the MIME type of the response).

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • 1
    Thank you @Aleksandr M for your input. When I tried entering UTF_8, it shows an error "UTF_8 cannot be resolved or is not a field". I tried including import java.nio.charset.Charset; as well. It still shows me an error. Also, could you tell me as to where are you retrieving the string in the ajax? – Gsm Jan 29 '15 at 22:43
  • @Gsm: If you are using java lower than 1.7 then just use `"some string".getBytes("UTF-8")`. The `data` in a success handler will have your string. BTW it is a good question, two upvotes clearly show that. :) – Aleksandr M Jan 30 '15 at 06:31
  • 1
    This, or use [JSON](http://stackoverflow.com/a/17149414/1654265) (with root = result). BTW you should upvote and accept this answer @Gsm – Andrea Ligios Jan 30 '15 at 09:27
  • @AndreaLigios: Well, OP asked especially about returning string and not json. BTW you can return json with the `stream` result also, in some places it is more convenient then using json plugin. ;) – Aleksandr M Jan 30 '15 at 09:32
  • Yes, but maybe he asked about String because is thinking that AJAX is already a mess without the need of adding the JSON mess. In that case, looking at the semplicity of the JSON plugin (or returning JSON with Stream result, in the other answer to the same question) might change what he think about this – Andrea Ligios Jan 30 '15 at 09:35
  • 1
    Maybe. BTW the other answer is slightly wrong, because of the mime type. :) – Aleksandr M Jan 30 '15 at 09:40
  • 1
    @AleksandrM Thank you for confirming that it is a good question. – Gsm Jan 30 '15 at 14:46
  • @AndreaLigios i accepted the answer :) – Gsm Jan 30 '15 at 14:46
  • @AleksandrM How about if i want to return a list of text strings? Is it possible only if I use json or can i do it without using json as well? – Gsm Feb 02 '15 at 23:24
  • 1
    @Gsm: Of course you can somehow custom parse your list of strings, but it is better to use json. Json is a de facto standard. – Aleksandr M Feb 03 '15 at 08:49