我正在开发一个JSF 2.0网络应用程序,并且在传递由不同单词组成的字符串时遇到了问题,到使用servlet的jQuery的AutoComplete。它在IE中很好用,但似乎无法检索Firefox中的任何数据。JavaScript在Firefox中使用jQuery UI失败自动完成
我的XHTML文件是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/WEB-INF/templates/base.xhtml">
<ui:define name="head">
<script>
$(document).ready(function(){
//var availableTags = "yes|maybe|no".split('|');
$.ajax({
type: 'POST',
cache: 'false',
data: 'codeType=allergyList',
url: '/miloWeb/Autocomplete',
async: false,
success: function(data){
availableTags = data.split('|');
},
error: function (xhr, ajaxOptions, thrownError){
alert(ajaxOptions);
}
});
$("#pAllergy").autocomplete({
source: availableTags
});
});
</script>
</ui:define>
<ui:define name="content">
<h2>Allergy List</h2>
<div>
<center>
<br />
<br />
<h:outputText styleClass="description" for="pAllergy">Allergy: </h:outputText>
<h:inputText id="pAllergy" value="#{allergyListBB.allergyList.allergy}" size="30"></h:inputText>
</tr>
<tr>
<td><button onclick="return false;" id="addAllergy">Add</button> </td>
</tr>
</table>
</center>
<h:inputHidden id="delId" value="#{allergyListBB.rowId}"></h:inputHidden>
</div>
</ui:define>
什么的代码最后一部分做的是进入我的Servlet和从数据库中检索的字符串(如奶酪|巧克力|奇瑞|板栗|)然后进入变量availableTags
,这是我们传入自动完成的。
我的servlet是这样的:
package com.bravo.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bravo.codeMaster.codeMaster;
import com.bravo.dropdowns.codeMedicationList;
/**
* Servlet implementation class Autocomplete
*/
@WebServlet("/Autocomplete")
public class Autocomplete extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Autocomplete() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getOutputStream().print("Unauthorized Access.");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String codeType = request.getParameter("codeType");
response.getOutputStream().print(codeMaster.getStringList(codeType));
// This might as well be "response.getOutputStream().print("one|two|three|")
// for testing purposes, just to make sure it's returning something.
}
}
XTRA评论:在我看来,如果我设置的变量availableTags
为字符串的任何字符串,那一刻我设置了$(“#pAllergy”)。 autocomplete(),我无法改变它。所以我需要从Servlet中获取正确的字符串。请帮助我,我似乎无法弄清楚为什么它在Firefox中失败,而不是在IE中。我非常感谢帮助!
您没有使用JSP。您正在使用其后继者Facelets。另外,问题不在JSF/Facelets方面,而在Servlet方面。我修复了不正确的JSP标记。 – BalusC