【问题标题】:Primefaces autocomplete not working and showing no exceptionPrimefaces 自动完成功能不起作用并且没有显示异常
【发布时间】:2012-08-28 17:11:23
【问题描述】:

我是 primefaces 的新手,我想使用 primefaces 的自动完成标签。所以我关注了this example
这是我的代码

layout.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"
      xmlns:p="http://primefaces.org/ui">
<head>

<title>Title</title>
</head>
<body>
<h:form id="form">  
    <p:panel header="AutoComplete" toggleable="true" id="panel">
        <h:panelGrid columns="2" cellpadding="5">  

            <h:outputLabel value="Simple :" for="acSimple" />  
            <p:autoComplete id="acSimple" value="#{autoCompleteBean.txt1}"   
                    completeMethod="#{autoCompleteBean.complete}"/>
                    </h:panelGrid>
                    </p:panel>
                    </h:form>
</body>
</html>

AutoCompleteBean.java

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="autoCompleteBean")
@RequestScoped
public class AutoCompleteBean {  

    private String txt1;  

    public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  

        for (int i = 0; i < 10; i++) {  
            results.add(query + i);  
        }  

        return results;  
    }  

    public String getTxt1() {  
        return txt1;  
    }  

    public void setTxt1(String txt1) {  
        this.txt1 = txt1;  
    }  
}

所以 layout.xhtml 呈现正常并向我显示一个文本字段,但之后它不起作用并且不显示自动完成功能。
是否缺少某些东西?或者会有什么问题
谢谢

【问题讨论】:

    标签: jsf jakarta-ee primefaces


    【解决方案1】:

    您发布的 xhtml 对 headbody 使用标准 html 标签,因此它可能不是正确解释用于调用 bean 中完整方法的 Javascript。

    尝试使用 h:headh:body

    提示可能会显示在您的输出窗口中。检查类似的东西:

    sourceId=null[severity=(ERROR 2), summary=(One or more resources have the target of 'head', but no 'head' component has been defined within the view.), detail=(One or more resources have the target of 'head', but no 'head' component has been defined within the view.)]
    

    查看关于 h:head in primefaces 的 Stack Overflow 讨论:What's the difference between <h:head> and <head> in Java Facelets?

    【讨论】:

      【解决方案2】:

      在编写 facelets 时,您应该始终使用 h:headh:body。原因是为了使自动完成工作需要 javascript,如果您不包含 h:head jsf 将无法正确放置 javascript。

      【讨论】:

        【解决方案3】:

        我遇到了类似的问题,但是在我的情况下,当我删除 p:autocomplete 标记周围的 p 标记时,问题得到了解决。

        以下代码不会抛出错误消息,但不会出现自动选择下拉菜单。删除 &lt;p&gt;&lt;/p&gt; 后一切正常。

        <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:p="http://primefaces.org/ui"
        xmlns:f="http://xmlns.jcp.org/jsf/core"
        xmlns:h="http://xmlns.jcp.org/jsf/html">
        <h:head>
        </h:head>
        <h:body>
            <h:form>
                <p>
                    <p:autoComplete id="place" value="#{addPlaceBean.place}"
                        completeMethod="#{autoCompletePlace.completePlace}" var="place"
                    itemLabel="#{place.city}, #{place.country}"
                    itemValue="#{place}" converter="placeConverter">
                    </p:autoComplete>
                </p>
            </h:form>
        </h:body>
        </html>
        

        【讨论】:

        • 你确定吗?我无法想象这一点。 p标签上没有导致问题的css吗?
        • 我现在用一个最小的例子试了一下(见上文),问题还是一样。
        猜你喜欢
        • 2012-05-18
        • 2021-09-20
        • 1970-01-01
        • 1970-01-01
        • 2016-09-07
        • 2017-02-28
        • 2020-11-14
        • 2014-03-11
        • 2014-01-16
        相关资源
        最近更新 更多