- jsp页面sendXML.jsp
- <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
- <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
- <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
- <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
- <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html:html lang="true">
- <head>
- <html:base />
- <title>sendXML.jsp</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- <script type="text/javascript">
- var xmlHttp;
- function createXMLHttpRequest()
- {
- if(window.ActiveXObject)
- {
- xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- else if(window.XMLHttpRequest)
- {
- xmlHttp=new XMLHttpRequest();
- }
- }
- function createXML()
- {
- var xml="<pets>";
- var options=document.getElementById("pets").childNodes;
- var option=null;
- for(i=0;i<options.length;i++)
- {
- option=options[i];
- if(option.selected)
- {
- xml=xml+"<type>"+option.value+"<//type>";
- }
- }
- xml=xml+"<//pets>";
- }
- function sendPetTypes()
- {
- createXMLHttpRequest();
- var xml=createXML();
- var url="../newsManagerAction.do?method=postXMLPets&timeStamp="+new Date().getTime();
- xmlHttp.open("POST",url,true);
- xmlHttp.onreadystatechange=handleStateChange;
- xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
- xmlHttp.send(xml);
- }
- function handleStateChange()
- {
- if(xmlHttp.readyState==4)
- {
- if(xmlHttp.status==200)
- {
- parseResults();
- }
- }
- }
- function parseResults()
- {
- var responseDiv=document.getElementById("serverResponse");
- if(responseDiv.hasChildNodes())
- {
- for(i=0;i<responseDiv.length;i++)
- {
- responseDiv.removeChild(responseDiv.childNodes[i]);
- }
- }
- var responseText=document.createTextNode(xmlHttp.responseText);
- responseDiv.appendChild(responseText);
- }
- </script>
- </head>
- <body>
- <h1>select types :</h1>
- <form action="#">
- <select id="pets" name="pets" multiple="multiple">
- <option>type1</option>
- <option>type2类型2</option>
- <option>type3</option>
- <option>type4类型4</option>
- </select>
- <input type="button" value="send xml" οnclick="sendPetTypes()">
- </form>
- <h2>Server Response</h2>
- <div id="serverResponse"></div>
- </body>
- </html:html>
- Struts中的Action extends DispatchAction 源码如下;
- public ActionForward postXMLPets(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- String xml =readXMLFormRequestBody(request);
- Document xmlDoc=null;
- try {
- xmlDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
- } catch (Exception e) {
- System.out.println("parseConfigurationException or SAXException ");
- e.printStackTrace();
- }
- NodeList selectedPetTypes=xmlDoc.getElementsByTagName("type");
- String type=null;
- String responseText="selected Pets:";
- for(int i=0;i<selectedPetTypes.getLength();i++)
- {
- type=selectedPetTypes.item(i).getFirstChild().getNodeValue();
- responseText+=" "+type;
- }
- response.setContentType("text/xml");
- PrintWriter out=response.getWriter();
- out.println(responseText);
- out.close();
- return null;
- }
- private String readXMLFormRequestBody(HttpServletRequest request)throws Exception
- {
- StringBuffer xml=new StringBuffer();
- String line=null;
- try {
- BufferedReader reader=request.getReader();
- while((line=reader.readLine())!=null)
- {
- xml.append(line);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return xml.toString();
- }
- 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.
- <select id="pets" name="pets" multiple="multiple">
- <option value="type">type1</option>
- <option value="type2类型2">type2类型2</option>
- <option value="type3">type3</option>
- <option value="type4类型4">type4类型4</option>
- </select>
- 此段代码没有给option设置value ,给value赋值...
测试发现:从request中获得的xml串是空的.....从而造成建立"文档对象"xmlDoc报异常"失败"
- xmlHttp.setRequestHeader=("Content-type","application/x-www-form-urlencoded;");
在"application/x-www-form-urlencode"串后边要加;号.
- function createXML()
- {
- var xml="<pets>";
- var options=document.getElementById("pets").childNodes;
- var option=null;
- for(i=0;i<options.length;i++)
- {
- option=options[i];
- if(option.selected)
- {
- xml=xml+"<type>"+option.value+"<//type>";
- }
- }
- xml=xml+"<//pets>";
- }
此javaScript函数最后应该加上return xml;
错误定位到readXMLFormReqeustBody(HttpServeletRequest request)