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

第一种,使用你所说的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(&, &);
      System..out.println(xmlValue);
      response.getWriter().write(xmlValue);
      return null;
}
struts.xml
<action name=updateViewCount class=struts.action.WebEvalAction method=updateViewCount>
</action>


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

该贴由koei转至本版2014-5-2 16:07:37




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