0

我是primefaces的新手,我想使用primeface的自动完成标签。所以我遵循了这个例子
这是我的代码

布局.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 呈现良好并向我显示一个文本字段,但之后它不起作用并且不显示自动完成功能。
有什么遗漏吗?或者会有什么问题
谢谢

4

3 回答 3

1

您发布的 xhtml 使用标准 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 讨论:Java Facelets 中的 <h:head> 和 <head> 有什么区别?

于 2012-08-28T17:47:48.460 回答
0

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

于 2012-08-28T17:50:48.003 回答
0

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

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

<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>
于 2017-10-06T10:29:48.573 回答