大家好,我在我的项目中使用 Ajax 将产品名称加载到一个选择框中,并在选择框 onchange 事件中使用 storename 的 onselect 事件。在这里,我使用 Ajax 来获取从 java 动作类到 jsp 的列表。我在 Jsp 和 Action 类中的代码如下。
<s:label value="Store Name : *" />
<s:select name="storeName" list="storeList" onchange="loadProduct(this.value)" listKey="storeId" listValue="storeName" headerKey="-1" headerValue="Select the Store" />
<s:label value="Product Name : *" />
<s:select name="productName" list="productList" listKey="productId" listValue="productName" />
function loadProduct(id){
var URL = "AjaxPopMyCycle.action?storeName="+id;
ajaxEditFunctionCall(URL);
}
function ajaxEditFunctionCall(URL){
try{
xmlHttp=new XMLHttpRequest();
}catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
if (xmlHttp.status == 200) {
if(xmlHttp.responseXML != null ){
showMessage(xmlHttp.responseXML);
}
}
}
}
xmlHttp.open("GET", URL, true);
xmlHttp.send(URL);
}
function showMessage(errorDisplayXML){
var checklist=document.Add.productName;
checklist.options.length=0;
if(xmlHttp.readyState==4){
if(errorDisplayXML.getElementsByTagName("rootElement")[0]!=null){
var rootElement = errorDisplayXML.getElementsByTagName("rootElement")[0];
var location = rootElement.getElementsByTagName("Message");
var locArr = location[0];
var locArr = " ";
var tempArr;
var tempArr1;
for(var i=0; i<location.length; i++){
tempArr = "";
tempArr1 = "";
locArr = location[i];
tempArr = locArr.getElementsByTagName("productId")[0].firstChild.nodeValue;
tempArr1 = locArr.getElementsByTagName("productnName")[0].firstChild.nodeValue;
checklist.options[i]= new Option(tempArr1,tempArr);
}
}else{
alert("errorDisplayXML Contains NULL");
}
}
}
以下操作类中的代码用于获取结果并加载到 XML 中,如下所示。
detailList 包含数据库中与商店相关的产品列表。
public String getDetails(List detailedList)throws Exception{
Element tempElem = null,
rootElem = null;
Text textElem = null;
document=new org.dom4j.dom.DOMDocument();
rootElem = document.createElement("rootElement");
Element errorElement = null;
List saveList = new ArrayList();
saveList = detailedList;
System.out.println("DetailedList:"+saveList.size());
if(saveList.size()>0){
try {
for(int i=0;i<saveList.size();i++){
Product aproduct = (Product )saveList.get(i);
errorElement = document.createElement("Message");
tempElem = document.createElement("productId");
textElem = document.createTextNode(aproduct .getProductId());
tempElem.appendChild(textElem);
errorElement.appendChild(tempElem);
tempElem = document.createElement("productName");
textElem = document.createTextNode(aproduct.getProductName());
tempElem.appendChild(textElem);
errorElement.appendChild(tempElem);
rootElem.appendChild(errorElement);
}
}catch (Exception e) {
tempElem = document.createElement("Message");
return parseToString(tempElem);
}
return parseToString(rootElem);
}
public String parseToString(Element node) throws Exception {
OutputFormat format = new OutputFormat();
StringWriter stringOut = new StringWriter();
XMLSerializer serial = new XMLSerializer(stringOut,format);
serial.asDOMSerializer();
serial.serialize(node);
return stringOut.toString();
}
我在我的动作类中导入了以下包。
导入 org.w3c.dom.Document;
导入 org.w3c.dom.Element;
导入 org.w3c.dom.Text;
导入 com.sun.org.apache.xml.internal.serialize.OutputFormat;
导入 com.sun.org.apache.xml.internal.serialize.XMLSerializer;
在过去 3 周内,它可以正常工作并具有正确的功能。
但它没有得到编译并在我的服务器中显示以下错误消息。
C:\Users\Desktop\Updated\Project\src\main\java\com\action \AjaxAction.java:[199,5] com.sun.org.apache.xml.internal.serialize.XMLSerializer 是 Sun 专有的 API 和可能会在未来的版本中删除
C:\Users\Desktop\Updated\Project\src\main\java\com\action \AjaxAction.java:[199,32] com.sun.org.apache.xml.internal.serialize.XMLSerialize r 是 Sun 专有 API并且可能在未来的版本中被删除
我的项目使用 Struts2、Jsp、Hibernate3 作为前端,使用 Mysql 服务器作为后端。我不知道要解决这个问题。
任何人都请帮我解决这个问题。提前致谢!!!。