ajax、jsp、dom4j

这篇文章跟上一篇,做了一些大的改动,主要是xml创建改用dom4j,用反射知识支持更多的javaBean

ok,不再废话,上代码

 

  • 1.js代码不同之处:

 

原代码:var stuList = stuDom.getElementsByTagName("student");

 

修改之后为:var stuList = stuDom.getElementsByTagName("childNode");

 

  •  2.servlet代码:
  • public class InfoServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		//------------------通用方法使用dom4j框架--------------------------------------
    		response.setCharacterEncoding("utf-8");
    		response.setContentType("application/xml");
    		PrintWriter out = response.getWriter();
    		List<Student>list = new QueryStu().getStu();
    		WriteXmlUtil.write(out,list,"bean.Student");
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		doGet(request, response);
    	}
    
    }
    
     
  • 3.WriteXmlUtil类,这个是修改的关键之处
  • package xmlUtil;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    
    public class WriteXmlUtil {
    
    	public static <T> void write(PrintWriter out, List<T> list, String beanName) {
    		// -----------取得beanName的get方法--------------------------
    		Class<?> clazz = null;
    		try {
    			clazz = Class.forName(beanName);
    		} catch (ClassNotFoundException e1) {
    			e1.printStackTrace();
    		}
    		Method methods[] = clazz.getMethods();
    		List<Method> mGetList = new ArrayList<Method>();// javaBean类的全部get方法
    		for (int i = 0; i < methods.length; i++) {
    			int flag = methods[i].getName().indexOf("get");
    			if (flag != -1) {
    				mGetList.add(methods[i]);
    			}
    		}
    		mGetList.remove(mGetList.size() - 1);// 去掉getClass()方法
    		// ---------------------------------------------
    
    		Document document = DocumentHelper.createDocument();
    		Element root = DocumentHelper.createElement("root");
    		document.setRootElement(root);// 设为根节点
    		for (int i = 0; i < list.size(); i++) {
    			Element ele = root.addElement("childNode");// 子节点
    			for (int j = 0; j < mGetList.size(); j++) {
    				Object obj = list.get(i);
    				Element id = ele.addElement(mGetList.get(j).getName());
    				try {
    					Object value = mGetList.get(j).invoke(obj);//利用方法反射得到value数据
    					id.setText(value.toString());//设置文本
    				} catch (IllegalArgumentException e) {
    					e.printStackTrace();
    				} catch (IllegalAccessException e) {
    					e.printStackTrace();
    				} catch (InvocationTargetException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    		OutputFormat format = new OutputFormat();
    		format.setEncoding("UTF-8");
    		XMLWriter writer = new XMLWriter(out, format);
    		try {
    			writer.write(document);
    			writer.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
     ok,用到知识很碎,但是这个类通用性很高,只要从数据库获取到一个list数据,还有对应的javaBean类,那么就可以生成对应的xml文件,并加到out对象反馈给客户端

 

你可能感兴趣的:(jsp,Ajax,xml,dom4j)