jsf - 从复合组件获取支持组件中的表单数据

标签 jsf jsf-2 composite-component

我的复合组件包含以下形式:

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

如何访问 <input jsf:id="sc#{cc.attrs.ItemSource.answerId}" /> 的值在我的支持组件中?我在重写的支持 bean 中尝试了以下操作 processUpdates方法。

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属性的纯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 - 从复合组件获取支持组件中的表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44602394/

相关文章:

java - 从 JSP 弹出窗口获取值后,JSP 页面值未填充

java - 如何将数据从 JSF 传递到 Java Applet

使用 JSF 验证更改 Css 样式

servlets - 从 servlet 过滤器重定向到 jsf 返回未呈现为 html 的实际 jsf 代码

java - 复合组件监听器仅在页面重新加载后工作

java - JSF - 从数据表中删除时出现空指针异常

jsf - 如何在两个p中独占选择一个单选按钮:selectOneRadios

javascript - 显示来自其他网站的内容并跟踪 URL

jsf - 在 View 中使用复合组件两次在 JSF 中重复组件 ID