[转帖]   struts2处理ajax请求_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3369 | 回复: 0   主题: [转帖]   struts2处理ajax请求        下一篇 
yongweiguo
注册用户
等级:上尉
经验:593
发帖:32
精华:7
注册:2013-3-11
状态:离线
发送短消息息给yongweiguo 加好友    发送短消息息给yongweiguo 发消息
发表于: IP:您无权察看 2013-3-18 10:13:23 | [全部帖] [楼主帖] 楼主

第一种,使用你所说的stream来进行服务端处理:

public class TextResult extends ActionSupport {

    private InputStream inputStream;

    public InputStream getInputStream() {

    return inputStream;

    }

    public String execute() throws Exception {

    inputStream = new StringBufferInputStream("ok");

    return SUCCESS;

    }

}

struts.xml:

<action name="text-result" class="actions.TextResult">

<result type="stream">

<param name="contentType">text/html</param>

<param name="inputName">inputStream</param>

</result>

</action>

这样,客户端向text-result发送请求时,请会返回ok这个字符流。

--------------------------------------------------------

第二种,通过你刚刚说的使用返回null来解决:

public String updateViewCount()throws Exception{

    HttpServletResponse response=ServletActionContext.getResponse();

    response.setContentType("text/xml");

    response.setCharacterEncoding("UTF-8");

    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1

    response.setHeader("Pragma","no-cache"); //HTTP 1.0

    response.setDateHeader ("Expires", 0); //prevents caching at the proxy server

    String xmlValue="";

    try{

    WebGame wg=webGameService.getObjectById(Integer.parseInt(id));

    wg.setWgViewCount(wg.getWgViewCount()+1);

    webGameService.modifyObject(wg);

    xmlValue="<sup ";

    xmlValue+="result='"+wg.getWgViewCount()+"' ";

    xmlValue+="good='"+wg.getWgBallot()+"' ";

    xmlValue+="id='"+id+"' ";

    xmlValue+="/>";

    }

    catch(Exception e){

    xmlValue="<sup ";

    xmlValue+="error='"+e.getMessage()+"' ";

    xmlValue+="/>";

    }

    xmlValue=xmlValue.replace("&", "&amp;");

    System..out.println(xmlValue);

    response.getWriter().write(xmlValue);

    return null;

}

struts.xml

<action name="updateViewCount" class="struts.action.WebEvalAction" method="updateViewCount">

</action>

第二种与第一种本质上是没有区别的,一个是自己手动来完成输出流,一个是通过struts2自己来完成。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论