【问题标题】:Get form data in Backing Component from Composite Component从复合组件获取支持组件中的表单数据
【发布时间】:2017-11-20 00:10:17
【问题描述】:

我的复合组件包含以下表单:

<cc:interface componentType="answerCompositeComponent">
    <cc:attribute name="AnswerType" type="code.elephant.domainmodel.AnswerType" required="true" />
    <cc:attribute name="ItemSource" type="code.elephant.domainmodel.Answer" required="true" />
    <cc:attribute name="QuestionId" type="java.lang.Long" required="true" />
</cc:interface>
<cc:implementation>
    <input jsf:id="sc#{cc.attrs.ItemSource.answerId}" />
</cc:implementation>

如何在我的支持组件中访问&lt;input jsf:id="sc#{cc.attrs.ItemSource.answerId}" /&gt; 的值?我在覆盖的processUpdates 方法中在我的支持bean 中尝试了以下内容。

Answer ItemSource = (Answer) getValueExpression("ItemSource").getValue(context.getELContext());
String formid = String.format("sc%d", ItemSource.getAnswerId());
String get = context.getExternalContext().getRequestParameterMap().get(formid);

String get 始终为空。有没有办法获取输入的值?

PS:我知道在 jsf 中使用纯 html 不是它的目的。我只是对如何实现我的计划感兴趣。

【问题讨论】:

    标签: jsf jsf-2 composite-component


    【解决方案1】:

    我从来没有用过带有jsf属性的纯html,所以不知道是否适用。

    通常,这是访问组合中嵌套组件的常用方法:

    <cc:interface componentType="answerCompositeComponent">
        <cc:attribute name="AnswerType" type="code.elephant.domainmodel.AnswerType" required="true" />
        <cc:attribute name="ItemSource" type="code.elephant.domainmodel.Answer" required="true" />
        <cc:attribute name="QuestionId" type="java.lang.Long" required="true" />
    </cc:interface>
    <cc:implementation>
        <h:inputText id="questionInput" binding="#{cc.input}" />
    
        <!-- maybe something like this might work
            <input jsf:id="questionInput" jsf:binding="#{cc.input}" />
        -->
    </cc:implementation>
    

    在哪里

    @FacesComponent("answerCompositeComponent")
    public class AnswerCompositeComponent extends UINamingContainer
    {
        private UIInput input;
    
        @Override
        public void processUpdates(FacesContext context)
        {
            super.processUpdates(context);
    
            Object value = input.getValue();
            Object localValue = input.getLocalValue();
            Object submittedValue = input.getSubmittedValue();
    
            // do your things with values 
        }
    
        public UIInput getInput()
        {
            return input;
        }
    
        public void setInput(UIInput input)
        {
            this.input = input;
        }
    }
    

    请注意,复合支持组件是 NamingContainer,因此更喜欢静态(或根本没有)嵌套组件 ID。避免使用动态 ID,除非您确实需要它们并且您确切地知道自己在做什么。

    【讨论】:

    • 非常感谢!这对我行得通。别说谎。否,如果它与问题有关 - 有没有办法告诉 jsf 使用 id 属性而不是 name 属性?我在这里得到了一个无线电输入列表,我需要用属性名称对其进行分组。
    • JSF 在生成的 html 中将 both id 和 name 设置为相同的值,用于 UIInput 组件。 html 容器
      本身需要 html 输入 name 属性,否则它将不知道在提交请求中传递哪些键值对以及如何传递键值对。也许我误解了你的要求?
    • 呵呵。是否也可以在processUpdates方法中获取组合组件生成的html代码,搜索&lt;input id="questionInput" 并将其值手册赋值给Answer模型?
    猜你喜欢
    • 2012-04-29
    • 2013-08-22
    • 2015-02-26
    • 2012-03-10
    • 1970-01-01
    • 2020-04-04
    • 2015-09-23
    • 2016-02-11
    • 2011-07-23
    相关资源
    最近更新 更多