我正在尝试使用 POJO 实现 PF Autocomplete 组件(v3.2)。当用户从提供的可能下拉选项中选择一个条目时,一切正常。但是,用户在输入字段中输入一些文本并简单地按 Enter 的情况对我来说效果不佳。
如果用户只是在自动完成输入框中输入一些文本并按回车键,我所要做的就是将它们重定向到具有 1 个查询参数的新页面。我注意到,如果我在AutoComplete 有机会显示建议之前按 Enter 键,那么重定向就会发生得很好。显示下拉建议后,按 Enter 即可清除输入框。
我希望这样的事情可以正常工作,所以我一定是在做一些愚蠢的事情。
<h:form>
<p:autoComplete id="globalAutoComplete" value="#{autoCompleteBackingBean.selectedResult}" completeMethod="#{autoCompleteBackingBean.globalSearch}" var="aResult" itemValue="#{aResult}" converter="autoCompleteConverter" queryDelay="200" maxResults="6" minQueryLength="3">
<p:column>
<a href="#{aResult.url}" style="position: relative; display:block; text-decoration: none;"> #{aResult.label} <br/> #{aResult.desc} <span class='ui-icon #{aResult.icon} autocompleteResultsIcon'/> </a>
</p:column>
</p:autoComplete>
<p:commandButton id="submitAutoComplete" type="submit" icon="ui-icon-suitcase" action="#{autoCompleteBackingBean.doRedirect}"/>
</h:form>
转换器:
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
if (value == null || value.length() == 0 ) {
return null;
}
Object obj = null;
try {
List<AutoComplete> results = AutoCompleteBackingBean.getResults();
for (AutoComplete aResult : results){
if((aResult.getDreRef()).equals(value))
return aResult;
}
} catch (Exception e) {
System.err.println("AutoCompleteConverter getAsObject Error: " + e);
}
if (obj == null) {
AutoComplete empty = new AutoComplete();
empty.setDreRef(value);
return empty;
}
return obj;
}
public String getAsString(FacesContext fc, UIComponent uic, Object object) {
if (object == null) {
return null;
}
Class entityClass = getEntityClass();
String value = null;
if (entityClass.isInstance(object)) {
try {
value = ((AutoComplete)object).getDreRef();
}catch (Exception e){
System.err.println("AutoCompleteConverter getAsString Error: " + e);
value = "Unable to obtain String from Object";
}
}
else {
value = "AutoCompleteConverter.getAsString(): Object " + object + " is of type "
+ object.getClass().getSimpleName() + "; expected type: "
+ entityClass.getSimpleName();
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, value, null));
}
return value;
}
支持豆:
private AutoComplete selectedResult = null;
//getter and setter
public String doRedirect() {
System.out.println("doRedirect()");
return "entities.jsf?query=" + selectedResult.getDreRef() + "&faces-redirect=true";
}