AJAX Struts 框架整合例子请求参数作为XML传送(-)

  1. jsp页面sendXML.jsp
  2. <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
  3. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
  4. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
  5. <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
  6. <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
  7. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html:html lang="true">
  10.   <head>
  11.     <html:base />
  12.     
  13.     <title>sendXML.jsp</title>
  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.   <script type="text/javascript">
  23.     var xmlHttp;
  24.     function createXMLHttpRequest()
  25.     {
  26.         if(window.ActiveXObject)
  27.         {
  28.             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  29.         }
  30.         else if(window.XMLHttpRequest) 
  31.         {
  32.             xmlHttp=new XMLHttpRequest();
  33.         }
  34.     }
  35.     function createXML()
  36.     {
  37.         var xml="<pets>";
  38.         var options=document.getElementById("pets").childNodes;
  39.         var option=null;
  40.         for(i=0;i<options.length;i++)
  41.         {
  42.             option=options[i];
  43.             if(option.selected)
  44.             {
  45.                 xml=xml+"<type>"+option.value+"<//type>";
  46.             }
  47.         }
  48.         xml=xml+"<//pets>";
  49.     }
  50.     function sendPetTypes()
  51.     {
  52.         createXMLHttpRequest();
  53.         
  54.         var xml=createXML();
  55.         var url="../newsManagerAction.do?method=postXMLPets&timeStamp="+new Date().getTime();
  56.         
  57.         xmlHttp.open("POST",url,true);
  58.         xmlHttp.onreadystatechange=handleStateChange;
  59.         xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  60.         xmlHttp.send(xml);
  61.     }
  62.     function handleStateChange()
  63.     {
  64.         if(xmlHttp.readyState==4)
  65.         {
  66.             if(xmlHttp.status==200)
  67.             {
  68.                 parseResults();
  69.             }
  70.         }
  71.     }
  72.     function parseResults()
  73.     {
  74.         var responseDiv=document.getElementById("serverResponse");
  75.         if(responseDiv.hasChildNodes())
  76.         {
  77.             for(i=0;i<responseDiv.length;i++)
  78.             {
  79.                 responseDiv.removeChild(responseDiv.childNodes[i]);
  80.             }
  81.         }
  82.         var responseText=document.createTextNode(xmlHttp.responseText);
  83.         responseDiv.appendChild(responseText);
  84.     }
  85.   </script>
  86.   </head>
  87.   
  88.   <body>
  89.     <h1>select types :</h1>
  90.     <form action="#">
  91.         <select id="pets"  name="pets" multiple="multiple">
  92.             <option>type1</option>
  93.             <option>type2类型2</option>
  94.             <option>type3</option>
  95.             <option>type4类型4</option>
  96.         </select>
  97.         <input type="button" value="send xml" οnclick="sendPetTypes()">
  98.     </form>
  99.     <h2>Server Response</h2>
  100.     <div id="serverResponse"></div>
  101.   </body>
  102. </html:html>
  103. Struts中的Action extends DispatchAction 源码如下;
  104.     public ActionForward postXMLPets(ActionMapping mapping, ActionForm form,
  105.             HttpServletRequest request, HttpServletResponse response)
  106.             throws Exception {
  107.         String xml =readXMLFormRequestBody(request);
  108.         Document xmlDoc=null;
  109.         try {
  110.             xmlDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
  111.         } catch (Exception e) {
  112.             System.out.println("parseConfigurationException or SAXException ");
  113.             e.printStackTrace();
  114.         }
  115.         NodeList selectedPetTypes=xmlDoc.getElementsByTagName("type");
  116.         String type=null;
  117.         String responseText="selected Pets:";
  118.         for(int i=0;i<selectedPetTypes.getLength();i++)
  119.         {
  120.             type=selectedPetTypes.item(i).getFirstChild().getNodeValue();
  121.             responseText+=" "+type;
  122.         }
  123.         response.setContentType("text/xml");
  124.         PrintWriter out=response.getWriter();
  125.         out.println(responseText);
  126.         out.close();
  127.         return null;
  128.     }
  129.     private String readXMLFormRequestBody(HttpServletRequest request)throws Exception 
  130.     {
  131.         StringBuffer xml=new StringBuffer();
  132.         String line=null;
  133.         try {
  134.             BufferedReader reader=request.getReader();
  135.             while((line=reader.readLine())!=null)
  136.             {
  137.                     xml.append(line);
  138.             }
  139.         } catch (Exception e) {
  140.             e.printStackTrace();
  141.         }
  142.         return xml.toString();
  143.     }
  144. struts-config.xml 配置文件中的配置<Action></Action> 就不多讲了.

出现问题及解决办法:

(1) [Fatal Error] :-1:-1: Premature end of file.
parseConfigurationException or SAXException
org.xml.sax.SAXParseException: Premature end of file.

  

  1.         <select id="pets"  name="pets" multiple="multiple">
  2.             <option value="type">type1</option>
  3.             <option value="type2类型2">type2类型2</option>
  4.             <option value="type3">type3</option>
  5.             <option value="type4类型4">type4类型4</option>
  6.         </select>
  7. 此段代码没有给option设置value ,给value赋值...

测试发现:从request中获得的xml串是空的.....从而造成建立"文档对象"xmlDoc报异常"失败"

  1. xmlHttp.setRequestHeader=("Content-type","application/x-www-form-urlencoded;");

在"application/x-www-form-urlencode"串后边要加;号.

  1.     function createXML()
  2.     {
  3.         var xml="<pets>";
  4.         var options=document.getElementById("pets").childNodes;
  5.         var option=null;
  6.         for(i=0;i<options.length;i++)
  7.         {
  8.             option=options[i];
  9.             if(option.selected)
  10.             {
  11.                 xml=xml+"<type>"+option.value+"<//type>";
  12.             }
  13.         }
  14.         xml=xml+"<//pets>";
  15.     }

此javaScript函数最后应该加上return xml;

错误定位到readXMLFormReqeustBody(HttpServeletRequest request)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值