调用一个Struts 2的动作用ajax,直接写一个字符串的响应不会返回字符串 [英] Call a Struts 2 action using ajax, which write a string directly to the response does not return the string
问题描述
在一个struts2的应用程序,我呼吁在一个Ajax请求,并直接写一个字符串到响应如下,并返回空
在动作的执行方法。
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;
在的struts.xml
我有以下声明,(以下为应用程序如何声明与工作正常结果类型的动作。在我的情况,因为我不吨需要结果调用JSP或其他操作,我没加的结果标签)
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">
和我映射了部分类的应用程序的context.xml
<bean id="controller" class="com.test.ControllerAction" scope="prototype">
然后,我有如下的Ajax调用,
Then I have the ajax call as below,
$.ajax({url:"/root/me/controller.action",success:function(result){
alert(result);
}});
但问题是在上面,而不是提醒我写了响应样本字符串
,它会提醒整个JSP页面,上面Ajax调用所在。我缺少的是在这里吗?
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?
推荐答案
返回的结果类型流
默认情况下,它输出的文本。
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
流
应的属性类型 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;
}
}
这篇关于调用一个Struts 2的动作用ajax,直接写一个字符串的响应不会返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!