2011年6月14日星期二

  【转】Struts2返回XML,JSON格式

Struts2返回XML格式
1.struts.xml里面的配置package extends="struts-default"

   
    "xxxName" 
    class=
    "xxxAction" method=
    "xxxMethod" >
   
   
               
   
    "xmlMessage" type=
    "plaintext">
     
           

2.Action里面的方法

//没有返回值
public void xxxMethod() throws IOException{
        HttpServletResponse response = ServletActionContext.getResponse();    
        PrintWriter out = response.getWriter();    
        response.setContentType("text/xml;charset=utf-8"); 
        response.setHeader("Cache-Control", "no-cache"); 
        out.write("你的XML文档");
//不用关,Struts2会帮你关
    }

Struts2返回Json格式
1、下载jsonplugin-0.7.jar包。下载地址是:http://code.google.com/p/jsonplugin/downloads/list
2、在struts.xml中包要继承json-default,如:extends="json-default"
3、返回时这样写:

   
    "json" name=
    "input">
   
       "root">msg
    

在action中有关于msg的get,set方法.msg可以是一个字符串,也可以是一个对象,里面的字段就对应着Key,Value

 

FROM  : http://langhua9527.iteye.com/blog/414309

此外:传值的文章还有:

http://blog.csdn.net/wbb1983/archive/2008/04/15/2294494.aspx

没有评论:

发表评论