Struts中利用action来输出xml文件
来源:互联网 发布:淘宝退货没退款怎么办 编辑:程序博客网 时间:2024/04/28 10:54
应一个同事的要求,要利用strtus的Action将一个数据库表的数据输出为xml格式的数据,方便导入使用。经研究Action还真能办到,实际上不是Action的功劳,而是JDOM的功劳,不说那么多了,给大家分享一下。
我有一个测试表,有两个字段,一个是用户名,一个是密码,现将这个表的数据全部输出为xml格式的。首先给出我的Action,代码如下:
package com.yourcompany.struts.action;import java.io.IOException;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.sql.DataSource;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.jdom.Document;import org.jdom.Element;import org.jdom.output.XMLOutputter;import com.yourcompany.struts.form.User;public class DisplayALL extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {DataSource ds = getDataSource(request, "s9");Connection conn = ds.getConnection();Statement stmt = null;ResultSet rs = null;response.setContentType("text/xml;charset=utf-8");Element rootElement = new Element("users");Document users = new Document(rootElement);try {String ls_sql = "select * from dd ";stmt = conn.createStatement();rs = stmt.executeQuery(ls_sql);while (rs.next()) {Element user = new Element("user");user.setAttribute("username", rs.getString(1));user.setAttribute("password", rs.getString(2));rootElement.addContent(user);}XMLOutputter xout=new XMLOutputter();xout.output(users,response.getWriter());} catch (SQLException e) {e.printStackTrace();} finally {rs.close();stmt.close();conn.close();}return null;}}
接下来说一下要注意的地方:
这里使用的JDOM的Document,要想使用JDOM,请在build Path中加入jdom.jar文件,否则找不到类。Document的构造函数的参数是一个 rootElement,
没有参数的构造函数不可见,Myeclipse提示:not visible.我参考一篇文档时,用的是不带参数的,不知道怎么回事?反正我测试时只有有参数的构造函数才能用。
关于strut中配置数据源的问题,我在以前的一篇文档中写过了,这里就不再啰嗦了。对了,一定要设置输出格式,为了防止中文乱码,最好给出字符集。
- Struts中利用action来输出xml文件
- 在struts.xml文件中使用spring代理,管理action
- 在Struts的action中使用Jdom生成xml文件
- struts.xml中action标签的学习
- struts.xml中Action的配置详解
- struts.xml中获得action中的数据
- 【Struts2文件下载】struts.xml文件中配置action,访问路径问题
- struts.xml中配置一个action调用另一个action
- struts2 spring集成时struts.xml文件中action的class属性值意义
- struts2 spring集成时struts.xml文件中action的class属性值意义
- struts2 spring集成时struts.xml文件中action的class属性值意义
- struts2 spring集成时struts.xml文件中action的class属性值意义
- struts2 spring集成时struts.xml文件中action的class属性值意义
- struts2 spring集成时struts.xml文件中action的class属性值意义
- struts2 spring集成时struts.xml文件中action的class属性值意义
- struts2在struts.xml文件中获取或设置action中的属性
- struts2 spring集成时struts.xml文件中action的class属性值意义
- struts2 spring集成时struts.xml文件中action的class属性值意义
- 5.11
- 关于vim的配置文件
- VC/C++技术面试题
- 老话重谈:大学英语四六级名师给2010年考生的15条箴言
- java log4j详解及其使用方法
- Struts中利用action来输出xml文件
- flex→鼠标放上去放大效果
- Linux下mplayer加载中文字幕乱码解决方案
- 解决Assert目录下无法拷贝超大文件到SD卡的问题
- beautiful English clauses
- WaitForSingleObject
- socket
- 为了四级。。。。。。
- VS2010 设置全局Include Directories与Library Directories