struts2支持直接将文件流返回到前台的,所以理论上你有好几种方法来实现生成xml
说默认的吧,只要将type="stream",这样你就可以输出xml、csv等等
1.先生成数据流
package com.jzlg.interce.action;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;
public class XMLAction extends ActionSupport {
private InputStream xmlStream; //定义一输入流
@Override
public String execute() throws Exception {
String result = "success";
StringBuffer buf = new StringBuffer();
//必须添加的,否则浏览器无法判断是否为xml文件
buf.append("<?xml version='1.0' encoding='UTF-8' ?>");
buf.append("<ff>");
buf.append("<aa>this is xml</aa>");
buf.append("<bb>this is xml</bb>");
buf.append("<ee>this is xml</ee>");
buf.append("</ff>");
try{
xmlStream = new ByteArrayInputStream(buf.toString().getBytes());
}catch(Exception e){
e.printStackTrace();
result = "false";
}
return result;
}
public InputStream getXmlStream() {
return xmlStream;
}
public void setXmlStream(InputStream xmlStream) {
this.xmlStream = xmlStream;
}
}
2.配置文件
<action name="exportXml" class="xmlaction">
<result name="success" type="stream">
<!-- 文件类型 -->
<param name="contentType">text/plain</param>
<!-- xmlStream 与对应action中的输入流的名字要一致 -->
<param name= "inputName">xmlStream</param>
</result>
</action>
3.完成
其实xml文件和html文件一样,都是纯文本的,也就是text/plain的,所有一般来讲还有个更BT的方法来实现输出xml的。
那就是action仍然做数据源的处理,不做文件封装(上面的代码实际action里做了xml文件的封装),结果返回到jsp,在jsp上做xml文件的展现,因为xml本身就是纯文本的。
两种方案,第一种在代码中做xml封装,这样就少写一个jsp页面,但改动的话,得重启服务器
第二种在jsp中做xml的封装,多写一个jsp
理论上,这种代码很少改,所以推荐用第一种方案。
另一种写法与上类似:
项目需要与FLEX进行通信,选用XML作为交互数据格式。
其中XML的生成可以使用一些java/xml绑定的开源项目,如xmlbeans来进行。若希望摆脱Servlet编程,使用Struts2直接生成XML文件输出,则可采用如下方式进行:
Struts.xml配置:
<action name="someAction" class="package.SomeAction" method="outXml">
<result type="plainText"></result>
</action>
Action 类:
public void outXml() throws IOException{
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=/"1.0/" encoding=/"UTF-8/" ?>");
sb.append("<users>");
sb.append(" <user id=/"50/">");
sb.append(" <userName>abc</userName>");
sb.append(" </user>");
sb.append("</users>");
//得到response
HttpServletResponse response = ServletActionContext.getResponse();
//设置编码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.write(sb.toString());
out.flush();
out.close();
}
通过读取本地的xml文件返回方法如下:
action中方法配置:
public void outXml() throws IOException{
File file=new File("f:\\struts.xml");
InputStream in=null;
in=new FileInputStream(file);
//得到response
HttpServletResponse response = ServletActionContext.getResponse();
//设置编码
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
//设置类型
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
Reader reader=new InputStreamReader(in,"UTF-8");
char [] by=new char[1024];
int temp=0;
while((temp=reader.read(by))!=-1)
{
out.write(by, 0, temp);
}
out.flush();
out.close();
}
struts配置文件中配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/">
<action name="exportXml" class="xmlaction" method="outXml">
<result type="plainText"></result>
</action>
</package>
</struts>
总结;以上的有自己的总结,也有别人的资料。个人觉得好的都整合在一起给大家借鉴