primefaces(3.2)autocomplete pojo

时间:2012-04-09 21:59:14

标签: autocomplete primefaces pojo

我正在尝试使用POJO实现PF自动完成组件(v3.2)。当用户从提供的可能下拉选项中选择条目时,工作正常。但是,用户在输入字段中输入一些文本而只是输入回车的情况对我来说效果不佳。

如果用户只是将一些文本输入到自动完成输入框并点击输入,我所要做的就是将它们重定向到带有1个查询参数的新页面。我注意到,如果我在之前输入,AutoComplete有机会显示建议,那么重定向就会发生。一旦显示下拉建议,点击输入只会清除输入框。

我希望这样的事情能够正常工作,所以我必须做些傻事。

<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;            
}

支持bean:

    private AutoComplete selectedResult = null;
    //getter and setter

    public String doRedirect() {
        System.out.println("doRedirect()");
        return "entities.jsf?query=" + selectedResult.getDreRef() + "&amp;faces-redirect=true";
    }

0 个答案:

没有答案