【问题标题】:How use primefaces autocomplete into composite with preselected values?如何使用 primefaces 自动完成与预选值的复合?
【发布时间】:2016-07-16 06:59:18
【问题描述】:

我在 UIInput 组合中使用自动完成 primefaces 组件时遇到问题。我的目标是使用自动完成字段中的预选值来初始化应用程序,并相应地显示一个标签。下面我展示一个测试代码

页面 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>

托管 Bean TestPage.xhtml

@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"});
   }
}

这种方法可以直接在 testPage.xhtml 上使用自动完成功能。但是,我想将此自动完成功能包装在 element 组合中,如下面的代码所示

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>

ElementComponent 复合背衬

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

但是当我在 testPage.xhtml 中包含元素组合时,自动完成不会显示预选值(与直接实现不同)。有没有办法解决这个问题? FacesComponent 的实现中可能缺少任何方法或属性?我倾向于认为这是primefaces实现和composite实现之间的一个错误,但我不确定。

【问题讨论】:

    标签: jsf primefaces autocomplete composite-component


    【解决方案1】:

    问题在于方法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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 2012-01-23
      • 2016-01-03
      • 2019-10-15
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      相关资源
      最近更新 更多