您的位置:首页 > 编程语言 > Java开发

struts2 和 ajax的交互

2015-08-19 16:02 489 查看
struts2的配置

<action name="getyzm" class="com.pwq.GetYZM">
<result name="success" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">textStream</param>
<param name="bufferSize">1024</param>
</result>
</action>


jsp的代码如下,定义了一个流type="stream",返回的是文件,textStream

<script type="text/javascript">
function captcha() {
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if(xmlhttp != null) {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById('testText').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", "getyzm.action", true);
xmlhttp.send();
} else alert("XMLHTTPREQUEST 创建失败!");
}
</script>


package com.ssh.pwq.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GetYZM extends ActionSupport{
private InputStream textStream;

public InputStream getTextStream() {
return textStream;
}

public void setTextStream(InputStream textStream) {
this.textStream = textStream;
}

public String execute() throws Exception {
HttpServletRequest request = null;
request = ServletActionContext.getRequest();
String name = (String)request.getSession().getAttribute("code");
System.out.println("name=>" + name);
textStream = new ByteArrayInputStream(name.getBytes("UTF-8"));
return SUCCESS;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: