关于Struts2返回XML,JSON格式配置记录
Struts2返回XML格式
1.struts.xml里面的配置package extends="struts-default" 或者extends="json-default" 都可以!
<action name="xxxName" method="xxxMethod" ><!-- 下面的name和type不能改动 --> <result name="xmlMessage" type="plaintext"></result> </action>
2.Action里面的方法 没有返回值void
//没有返回值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.34.jar包。下载地址是:http://code.google.com/p/jsonplugin/downloads/list
2、在struts.xml中包要继承json-default,如:extends="json-default"
3、返回时这样写:
<result type="json" name="json"> <param name="root">jsonResult</param></result>
?在action中有关于jsonResult的get,set方法.jsonResult可以是一个字符串,也可以是一个对象,里面的字段就对应着Key,Value !