:当我们在进行web开发的时候,服务端会发送很多数据信息到客户端。那么用什么容器去承载数据信息了,答案就是XmlXml是一种很标准的数据信息存储容器。不过现在有很多其他的方法,向客户端发送数据。如:JSON
1.首先我们要将数据信息存储在一个xml文件中
2.在struts2中通过ActionServletContext类的getResponse()方法获取一个 HttpServletResponse对象
通过HttpServletResponse对象的getWriter()方法获取PrintWriter对象 之后使用XmlWriter 将xml文档对象 写入到PrintWriter中 之后返回给客户端
: 1.struts2必要的jar包
2.dom4j的jar包
3.jquery需要的js文件
dom4j下载地址
JavaBean(Person) package org.viancent.domain; public class Person { private int pid; private String pname; private int page; private String address; public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Action:(GetXmlAction)
package org.viancent.action; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import org.viancent.domain.Person; import com.opensymphony.xwork2.ActionSupport; public class GetInfoAction extends ActionSupport { private static final long serialVersionUID = 1L; private String name; // 此属性用于接收页面的数据信息进行判断 返回结果 public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String execute() throws Exception { // create new person // zhangsan Person person1 = new Person(); person1.setPid(1); person1.setPname("zhangsan"); person1.setPage(20); person1.setAddress("武汉"); // lisi Person person2 = new Person(); person2.setPid(2); person2.setPname("lisi"); person2.setPage(30); person2.setAddress("上海"); // create new XmlObject use dom4j // 1.get document by documenthelper Document document = DocumentHelper.createDocument(); // create a comment document.addComment("This is a Xml!"); // create root Element Element rootElement = document.addElement("Users"); // create child Element by root Element Element childElement = rootElement.addElement("User"); // create id Element by child Element Element idElement = childElement.addElement("id"); // create name Element by child Element Element nameElement = childElement.addElement("name"); // create age Element by child Element Element ageElement = childElement.addElement("age"); // create address Element by child Element Element addressElement = childElement.addElement("address"); // check name if ("zhangsan".equals(this.getName())) { idElement.setText(person1.getPid() + ""); nameElement.setText(person1.getPname()); ageElement.setText(person1.getPage() + ""); addressElement.setText(person1.getAddress()); } else { idElement.setText(person2.getPid() + ""); nameElement.setText(person2.getPname()); ageElement.setText(person2.getPage() + ""); addressElement.setText(person2.getAddress()); } // use XMLWriter and HttpServletResponse // get HttpServletResponse HttpServletResponse response = ServletActionContext.getResponse(); // 设置返回文本的类型 response.setContentType("text/xml ; charset=utf-8"); /* * 这里一定要设定ContentType,不然客户端无法辨识服务端的发送的信息类型。 */ response.setHeader("Cache-Control", "no-cache"); /* * 设定不需要缓存 */ // get PrintWriter PrintWriter out = response.getWriter(); // 创建排版规则 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8");// 设置xml文档的编码格式 // 开始写入数据信息 XMLWriter writer = new XMLWriter(out, format); // 通过XmlWriter的构造函数,将Xml文档对象写入到PrintWriter中,实现返回 writer.write(document); // 清空流 out.flush(); // 关闭流 out.close(); return null;// 因为是使用jquery进行的异步提交,所以不需要进行跳转 } }
struts.xml
<package name="struts2" extends="struts-default"> <action name="getinfos" class="org.viancent.action.GetXmlAction"> </action> //这里不需要定义Action的<result>标签 因为是异步提交的方式 </package>
JSP:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="WEB-JS/jquery-1.5.1.js"></script> <script type="text/javascript"> function getInfo() { $.post("/struts2/getjsons.action",//指定异步提交的地址 { name:$("#info").val() //获取<select>中选定的<option>的值 用于传给客户端 }, function(returnedData, status) { if ("success" == status) { var id = $(returnedData).find("id").text(); /* $(returnedData).find("id").text() 获取returnedData中id标签的中的内容 */ var name = $(returnedData).find("name").text(); var age = $(returnedData).find("age").text(); var address = $(returnedData).find("address").text(); var html = "<table width='60%' align='center' border='1'><tr><th>ID</th><th>NAME</th><th>AGE</th><th>ADDRESS</th></tr><tr align='center'><td>" + id + "</td><td>" + name + "</td><td>" + age + "</td><td>" + address + "</td></tr></table>" $("body table:eq(0)").remove();//移除Body中的第一个table $("#theBody").append(html);//将创建的table信息添加到body的尾部 } }); } </script> </head> <body id="theBody"> <select id="info"> <%--选择获取谁的信息--%> <option value="zhangsan" selected="selected">zhangsan</option> <option value="lisi">lisi</option> </select> <input type="button" value="GetInfo" onclick="getInfo();"> </body> </html>