在 struts2 应用程序中,我调用 Ajax 请求并将字符串直接写入响应,如下所示并返回null
在操作的执行方法中。
ServeletActionContext.getResponse().getOutputStream().print("sample string");
return null;
In struts.xml
我有以下声明,(下面是应用程序如何声明具有运行良好的结果类型的操作。在我的情况下,因为我不需要结果来调用 JSP 或其他操作,所以我没有添加结果标签)
<action name="controller" class="controller">
我将节类映射到application-context.xml
<bean id="controller" class="com.test.ControllerAction" scope="prototype">
然后我有如下的ajax调用,
$.ajax({url:"/root/me/controller.action",success:function(result){
alert(result);
}});
但问题出在上面而不是提醒"sample string"
这是我为响应编写的,它会提醒上述 Ajax 调用所在的整个 JSP 页面。我在这里缺少什么?
返回结果类型stream
默认情况下它输出文本。
<action name="controller" class="ControllerAction">
<result type="stream">
<param name="contentType">text/html</param>
<param name="inputName">stream</param>
</result>
</action
stream
应该是属性类型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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)