struts2实现XML异步交互
异步交互,在不用重新提交整个页面的情况下可以实现页面局部信息与服务器的交互。在编写异步交互时需要用到一个架包:dom4j,下载地址为:https://dom4j.github.io/
下面通过例子说明struts异步交互的实现过程.
1、首先看一下文件目录
2、代码实现:
1) 首先创建一个Person类,该类用来映射个人信息
1 package com.action; 2 3 public class Person { 4 5 private int id; 6 private String name; 7 private int age; 8 private String address; 9 public int getId() { 10 return id; 11 } 12 public void setId(int id) { 13 this.id = id; 14 } 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public int getAge() { 22 return age; 23 } 24 public void setAge(int age) { 25 this.age = age; 26 } 27 public String getAddress() { 28 return address; 29 } 30 public void setAddress(String address) { 31 this.address = address; 32 } 33 34 }
2)GetXMLAction类
1 package com.action; 2 3 import java.io.PrintWriter; 4 5 import javax.servlet.http.HttpServletResponse; 6 7 import org.apache.struts2.ServletActionContext; 8 import org.dom4j.*; 9 import org.dom4j.io.OutputFormat; 10 import org.dom4j.io.XMLWriter; 11 12 import com.opensymphony.xwork2.ActionSupport; 13 14 @SuppressWarnings("serial") 15 public class GetXMLAction extends ActionSupport { 16 17 private String name; 18 19 public String getName() { 20 return name; 21 } 22 23 public void setName(String name) { 24 this.name = name; 25 } 26 27 @Override 28 public String execute() throws Exception { 29 //zhangsan 30 Person person = new Person(); 31 32 person.setId(1); 33 person.setName("zhangsan"); 34 person.setAge(30); 35 person.setAddress("beijing"); 36 37 //lisi 38 Person person2 = new Person(); 39 40 person2.setId(2); 41 person2.setName("lisi"); 42 person2.setAge(50); 43 person2.setAddress("tianjin"); 44 //创建一个Document对象 45 Document document = DocumentHelper.createDocument(); 46 //创建根元素 47 Element rootElement = document.addElement("persons"); 48 //增加注释 49 rootElement.addComment("This is comment"); 50 //向根元素中添加子元素 51 Element e = rootElement.addElement("person"); 52 53 Element idElement = e.addElement("id"); 54 Element nameElement = e.addElement("name"); 55 Element ageElement = e.addElement("age"); 56 Element addressElement = e.addElement("address"); 57 58 if("zhangsan".equals(name)){ 59 idElement.setText(person.getId() + ""); 60 nameElement.setText(person.getName()+""); 61 ageElement.setText(person.getAge()+""); 62 addressElement.setText(person.getAddress()+""); 63 }else{ 64 65 idElement.setText(person2.getId() + ""); 66 nameElement.setText(person2.getName()+""); 67 ageElement.setText(person2.getAge()+""); 68 addressElement.setText(person2.getAddress()+""); 69 } 70 //获取HttpServletResponse对象 71 HttpServletResponse response = ServletActionContext.getResponse(); 72 //设置文档内容类型 73 response.setContentType("text/xml;charset=GB18030"); 74 //设置http响应头禁用浏览器的缓冲 75 response.setHeader("cache-control", "no-cache"); 76 77 PrintWriter out = response.getWriter(); 78 //格式化xml 79 OutputFormat format = OutputFormat.createPrettyPrint(); 80 //指定编码方式编码 81 format.setEncoding("GB18030"); 82 83 XMLWriter writer = new XMLWriter(out,format); 84 85 writer.write(document); 86 87 out.flush(); 88 out.close(); 89 return null; 90 } 91 }
3) getXML.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'getXML.jsp' starting page</title> 13 <script type="text/javascript" src="scripts/jquery-1.4.4.js"></script> 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 <script type="text/javascript"> 24 25 function getInfo(){ 26 27 $.post("getXMLAction.action", 28 { 29 name:$("#name").val() 30 },function(returnedData,status) 31 { 32 var id= $(returnedData).find("id").text(); 33 var name = $(returnedData).find("name").text(); 34 var age = $(returnedData).find("age").text(); 35 var address = $(returnedData).find("address").text(); 36 37 var html = "<table width='60%' border='1' align='center'><tr><th>id</th><th>name</th><th>age</th><th>address</th><tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+age+"</td><td>"+address+"</td></tr></tr></table>"; 38 39 $("#theBody table:eq(0)").remove();//找到id为theBody的body中的第0个table(即第一个table表)将其的内容删除掉,防止出现累加 40 $("#theBody").append(html);//将构建的HTML加入到id为theBody的body中 41 42 43 }); 44 45 } 46 47 </script> 48 </head> 49 50 <body id="theBody"> 51 52 <select id="name"> 53 54 <option value="zhangsan">zhangsan</option> 55 <option value="lisi">lisi</option> 56 57 </select> 58 59 <input type="button" value="get information" onclick="getInfo();"> 60 61 </body> 62 </html>
4) struts.xml中的配置信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <!-- 设置Struts运行模式为开发者模式,如果value为false则关闭开发者模式 --> 7 <constant name="struts2.devMode" value="true"></constant> 8 9 <package name="struts2_ajax" namespace="/" extends="struts-default"> 10 11 <action name="getXMLAction" class="com.action.GetXMLAction"> 12 13 </action> 14 15 16 </package> 17 </struts>
web.xml总的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>struts2_ajax</display-name> 4 <welcome-file-list> 5 <welcome-file>index.jsp</welcome-file> 6 </welcome-file-list> 7 <filter> 8 <filter-name>testStruts2</filter-name> 9 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 10 </filter> 11 <filter-mapping> 12 <filter-name>testStruts2</filter-name> 13 <url-pattern>/*</url-pattern> 14 </filter-mapping> 15 </web-app>
运行结果: