目前我们都用Spring + Struts2,所以不到万不得已是不会用到servlet的,那么有时我们要像之前直接将内容输出到网页怎么办呢?(当前流行的Ajax技术之前很多是基于servlet与服务器交互取得信息的)

       其实在Struts2中我们可以这样来使用,达到的效果与servlet是一样的,代码如下:

java 代码
  1. public void ajax(){   
  2.        
  3.     PrintWriter pw = null;   
  4.     HttpServletResponse response = ServletActionContext.getResponse(); //取response对象   
  5.     try {   
  6.         response.setContentType("text/xml;charset=utf-8");       
  7.         response.setHeader("Cache-Control","no-cache");       
  8.               
  9.         pw = response.getWriter();   
  10.         pw.print("<root></root>");   
  11.         pw.print("");   
  12.         pw.print("");   
  13.     } catch (IOException e) {   
  14.         log.error(e.getMessage());   
  15.         e.printStackTrace();   
  16.     } finally{   
  17.         if(pw != null){   
  18.             pw.close();   
  19.             pw = null;   
  20.         }   
  21.     }   
  22. }  

        上面代码的部分内无法显示,详细见附图:

Struts2环境下如何直接将内容输出到网页(Ajax相关)

        这种办法还不错吧,也是从javaeye上一位网友的文章中看到的,呵呵!

相关文章:

  • 2021-12-15
  • 2021-07-10
  • 2021-12-24
  • 2021-12-09
  • 2021-05-03
  • 2022-01-18
  • 2021-10-26
  • 2022-12-23
猜你喜欢
  • 2021-10-24
  • 2022-12-23
  • 2021-09-06
  • 2021-06-24
  • 2022-01-31
  • 2021-08-16
  • 2022-03-11
相关资源
相似解决方案