【问题标题】:How do I define a component as an attribute of composite component?如何将组件定义为复合组件的属性?
【发布时间】:2014-04-22 13:55:54
【问题描述】:

我正在创建一个带有一些特定符号的键盘(复合组件),用户可以在文本字段中插入这些符号。我需要定义两个组件作为键盘的属性,p:inputText 和 p:inputTextarea,将用户选择的符号设置为这些输入的值。问题是我不能使用组件作为键盘属性,它不管用。在我的 index.xhtml 文件中,如何传递 inputText? 索引:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:pe="http://java.sun.com/jsf/composite/pe"
    template="/pages/template/template.xhtml">
    <ui:define name="content">


        <p:inputText widgetVar="asd" id="asd" value="#{disciplineController.discipline.description}"/>
        <pe:keyboard textField="asd"/>

<!-- How do I pass the p:inputText as attribute? 
Already tried <pe:keyboard textField="#{asd}"/>, it gets null
-->


    </ui:define>
</ui:composition>

键盘

<composite:interface componentType="keyboardComponentBean">
    <composite:attribute name="textField" type="org.primefaces.component.inputtext.InputText"></composite:attribute>
    <composite:attribute name="textFieldArea" type="org.primefaces.component.inputtextarea.InputTextarea"></composite:attribute>
</composite:interface>

<composite:implementation>
    <h:form>
            <ui:repeat value="#{cc.attrs.SYMBOL_ARRAY}" var="symbol">
            <ui:fragment rendered="#{symbol == cc.attrs.SYMBOL_ARRAY[0]}"> Conjuntos <br /></ui:fragment>
            <ui:fragment rendered="#{symbol == cc.attrs.SYMBOL_ARRAY[11]}"> <br />Símbolos <br /></ui:fragment>
            <ui:fragment rendered="#{symbol == cc.attrs.SYMBOL_ARRAY[24]}"> <br />Subscrito <br /></ui:fragment>
            <ui:fragment rendered="#{symbol == cc.attrs.SYMBOL_ARRAY[39]}"> <br />Sobrescrito <br /></ui:fragment>
            <ui:fragment rendered="#{symbol == cc.attrs.SYMBOL_ARRAY[55]}"> <br />Grego (maiúsculo) <br /></ui:fragment>
            <ui:fragment rendered="#{symbol == cc.attrs.SYMBOL_ARRAY[65]}"> <br />Grego (minúsculo) <br /></ui:fragment>
            <ui:fragment rendered="#{symbol == cc.attrs.SYMBOL_ARRAY[81]}"> <br />Setas <br /></ui:fragment>
            <p:commandButton value="#{symbol}" action="#{cc.addText(symbol)}" />
            </ui:repeat>
    </h:form>
</composite:implementation>
</html>

FacesComponent:

package br.com.portal.education.Timer;

import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;

import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.inputtextarea.InputTextarea;

@FacesComponent("keyboardComponentBean")
public class Keyboard extends UINamingContainer {

    private final String[] SYMBOL_ARRAY = {..símbolos..};

    public String[] getSYMBOL_ARRAY() {
        return SYMBOL_ARRAY;
    }

    public void addText(String symbol) {
        Object textField = getAttributes().get("textField");
        Object textFieldArea = getAttributes().get("textFieldArea");
            if (textField != null){
                ((InputText) textField).setValue(((InputText) textField).getValue() + symbol);
            }

            if (textFieldArea != null){
            ((InputTextarea) textFieldArea).setValue(((InputTextarea) textFieldArea).getValue() + symbol);
        }
    }
}

【问题讨论】:

    标签: jsf composite-component


    【解决方案1】:

    您可以将输入组件绑定到 EL 表达式:

    <p:inputText widgetVar="asd" id="asd" binding="#{bindedAsd}" value="#{disciplineController.discipline.description}"/>
    

    这样你就可以把它当成一个属性来使用了:

    <pe:keyboard textField="#{bindedAsd}" />
    

    【讨论】:

    • 它有效,谢谢!但是 textField 变量中的 getValue() 方法总是返回 null,即使我输入了一些东西。你知道会是什么吗?
    • 因为您不将其处理到服务器,所以它永远不会获得价值。将此代码添加到您的命令按钮:update=":#{textField.clientId}"
    • 使用:,我收到错误消息Cannot find component with expression ":" referenced from "j_idt94:j_idt95:j_idt96:0:j_idt111".。删除时有效:update="#{textField.clientId}",但我仍然无法设置 textField 值。我猜我使用了错误的方法来获取/设置它,因为它在调试时总是为空......你能启发我吗?
    • 对不起,我的意思是写 process="..." 而不是 update="..."
    • 没问题 :) 但是现在,有了process=":#{textField.clientId}",我得到了这个javax.servlet.ServletException: Cannot find component with expression ":" referenced from "j_idt94:j_idt95:j_idt96:0:j_idt111".。删除:,它会渲染页面但它不起作用...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2017-10-10
    • 2011-06-02
    • 2013-05-25
    • 2020-04-18
    • 1970-01-01
    • 2017-11-11
    相关资源
    最近更新 更多