Jsf 如何使用primefaces自动完成到具有预选值的合成中?
我在UIInput组合中使用autocomplete primefaces组件时遇到问题。我的目标是在autocomplete字段中使用预选值初始化应用程序,并相应地显示一个标签。下面我展示一个测试代码 页面测试页面.xhtmlJsf 如何使用primefaces自动完成到具有预选值的合成中?,jsf,primefaces,autocomplete,composite-component,Jsf,Primefaces,Autocomplete,Composite Component,我在UIInput组合中使用autocomplete primefaces组件时遇到问题。我的目标是在autocomplete字段中使用预选值初始化应用程序,并相应地显示一个标签。下面我展示一个测试代码 页面测试页面.xhtml <f:view id="view" locale="#{webSession.currentLanguage.locale}"> <h:head> <title>...</title> &
<f:view id="view" locale="#{webSession.currentLanguage.locale}">
<h:head>
<title>...</title>
</h:head>
<h:body>
<h:form>
<utils:element/>
<p:autoComplete
value="#{testPage.attr}"
completeMethod="#{testPage.completeMethod}"
var="item"
itemLabel="#{item}"
itemValue="#{item}" />
</h:form>
</h:body>
</f:view>
@ManagedBean(name = "testPage")
@ViewScoped
public class TestPage {
private String attr;
@PostConstruct
public void init(){
attr = "value 1";
}
public String getAttr() {
return attr;
}
public void setAttr(String attr) {
this.attr = attr;
}
public List<String> completeMethod(String query) {
return Arrays.asList(new String[]{"1111", "2222", "3333"});
}
}
...
托管Bean TestPage.xhtml
<f:view id="view" locale="#{webSession.currentLanguage.locale}">
<h:head>
<title>...</title>
</h:head>
<h:body>
<h:form>
<utils:element/>
<p:autoComplete
value="#{testPage.attr}"
completeMethod="#{testPage.completeMethod}"
var="item"
itemLabel="#{item}"
itemValue="#{item}" />
</h:form>
</h:body>
</f:view>
@ManagedBean(name = "testPage")
@ViewScoped
public class TestPage {
private String attr;
@PostConstruct
public void init(){
attr = "value 1";
}
public String getAttr() {
return attr;
}
public void setAttr(String attr) {
this.attr = attr;
}
public List<String> completeMethod(String query) {
return Arrays.asList(new String[]{"1111", "2222", "3333"});
}
}
@ManagedBean(name=“testPage”)
@视域
公共类测试页{
私有字符串属性;
@施工后
公共void init(){
attr=“值1”;
}
公共字符串getAttr(){
返回属性;
}
公共void setAttr(字符串attr){
this.attr=attr;
}
公共列表完成方法(字符串查询){
返回Arrays.asList(新字符串[]{“1111”、“2222”、“3333”});
}
}
这种方法可以直接在testPage.xhtml上使用自动完成。然而,我想将这个自动完成包装在一个元素组合中,如下面的代码所示
element.xhtml复合页面
<ui:component xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface componentType="elementComponent">
</cc:interface>
<cc:implementation>
<p:autoComplete
value="#{cc.attr}"
completeMethod="#{cc.completeMethod}"
var="item"
itemLabel="#{item}"
itemValue="#{item}" />
</cc:implementation>
</ui:component>
元素组件复合背衬
@FacesComponent("elementComponent")
@ViewScoped
public class ElementComponent extends UIInput implements NamingContainer{
private String attr;
@Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
public List<String> completeMethod(String query) {
return Arrays.asList(new String[]{"value 1", "value 2", "value 3"});
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
attr = "value 1";
}
public String getAttr() {
return attr;
}
public void setAttr(String attr) {
this.attr = attr;
}
}
@FacesComponent(“elementComponent”)
@视域
公共类ElementComponent扩展UIInput实现NamingContainer{
私有字符串属性;
@凌驾
公共字符串getFamily(){
返回UINamingContainer.COMPONENT_族;
}
公共列表完成方法(字符串查询){
返回Arrays.asList(新字符串[]{“值1”、“值2”、“值3”});
}
@凌驾
public void encodeBegin(FacesContext上下文)引发IOException{
attr=“值1”;
}
公共字符串getAttr(){
返回属性;
}
公共void setAttr(字符串attr){
this.attr=attr;
}
}
但是,当我在testPage.xhtml中包含元素composite时,autocomplete不会显示预选值(与直接实现不同)。有什么办法解决这个问题吗?FacesComponent的实现中可能缺少任何方法或属性?我倾向于认为这是primefaces实现和composite实现之间的一个缺陷,但我不确定。问题在于encodeBegin()方法。此实现需要对组件类进行编码,并对父级(UIInput)进行编码 不正确
@Override
public void encodeBegin(FacesContext context) throws IOException {
attr = "value 1";
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
attr = "value 1";
super.encodeBegin();
}
正确
@Override
public void encodeBegin(FacesContext context) throws IOException {
attr = "value 1";
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
attr = "value 1";
super.encodeBegin();
}