2

In a struts2 application I am calling a Ajax request in and write a string directly to the response as below and return null in the execute method of the action.

ServeletActionContext.getResponse().getOutputStream().print("sample string");
return null;

In struts.xml I have the below declaration, (below is how the application declares the actions with result types which are working fine. In my case since I don't need result to invoke a JSP or another action, I did not add the result tag)

<action name="controller" class="controller">

And I map the section class in the application-context.xml

<bean id="controller" class="com.test.ControllerAction" scope="prototype">

Then I have the ajax call as below,

$.ajax({url:"/root/me/controller.action",success:function(result){
    alert(result);
}});

But the problem is in above instead of alerting the "sample string" which I wrote for the response, it alerts the whole JSP page where the above Ajax call resides. What am I missing here?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Harshana
  • 7,297
  • 25
  • 99
  • 173

2 Answers2

4

Return result type stream by default it outputs text.

<action name="controller" class="ControllerAction">
  <result type="stream">
    <param name="contentType">text/html</param>
    <param name="inputName">stream</param>
  </result>
</action

stream should be the property type ImputStream;

public class ControllerAction extends ActionSupport {

  private InputStream stream;

  //getter here
  public InputStream getStream() {
    return stream;
  }

  public String execute() throws Exception {
    String str = "sample string";
    stream = new ByteArrayInputStream(str.getBytes());
    return SUCCESS;
  }
}    
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • do i have to mentioned param names also since the response directly go the ajax call back function? – Harshana Jun 16 '13 at 16:24
  • param names are necessary part of the result tag it will tell the result builder where to get the input and how to expose content type to response. But in the callback function you'll get raw data only. – Roman C Jun 16 '13 at 16:41
  • Thanks above work. Can you please tell me if i want to get a json object as above can i construct the json notation as a string and user jQuery.getJSON method. I tried it but could not able to. I guess there should be additional configaration need to mentioned. Can you please tell those? – Harshana Jun 19 '13 at 12:52
  • @Harshana No, jQuery.getJSON loads JSON-encoded data, this data is raw and needs to parse on the client via jQuery.parseJSON. However if you change the content type to "application/json" then it could work. See [this](http://stackoverflow.com/questions/17093862/jquery-ajax-issue-returning-json-value/17096564#17096564) answer. – Roman C Jun 19 '13 at 15:38
0

Instead of using

ServeletActionContext.getResponse().getOutputStream().print("sample string");
return null;

use this code

PrintWriter out = response.getWriter();
out.write("sample string");
return null;

This should work.

Sithum
  • 126
  • 7