Call a Struts 2 action using ajax, which write a string directly to the response does not return the string
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;
}
}
Harshana
Software Engineer (Specialized J2EE) based on Colombo Sri Lanka
Updated on June 04, 2022Comments
-
Harshana almost 2 years
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? -
Harshana almost 11 yearsDo i have to specify a result tag in the action?
-
Harshana almost 11 yearsI still find the same issue
-
Harshana almost 11 yearsdo i have to mentioned param names also since the response directly go the ajax call back function?
-
Roman C almost 11 yearsparam 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.
-
Harshana almost 11 yearsThanks 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?
-
Roman C almost 11 years@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 answer.
-
Ajay Takur almost 7 years@Harshana You do specify <result type="stream"> <param name="contentType">text/html</param> <param name="inputName">stream</param> </result>