【问题标题】:JSF reading dynamic input element in a managed beanJSF 读取托管 bean 中的动态输入元素
【发布时间】:2011-12-24 09:41:59
【问题描述】:

我有一个非常复杂的 JSF 页面(我们使用带有 facelet 的 JSF2),我必须在其中“插入”一个纯 html 表单部分(它代表一个所见即所得的模板,用于稍后将创建为 Pdf 的文档)。 非常简化的页面看起来像

<h:form id="formEditDoc">
   <p:commandButton styleClass="commandButton" value="Save"
      actionListener="#{myBean.myAction}" update="masterForm:msg">
   </p:commandButton>

   <!-- some jsf controls here -->
   ....

   <!-- this is my dynamic section -->
   <input id="ft2" type="text" value="foo"/>
</h:form>

在托管 bean myBean(请求范围)中,我有一个动作侦听器,我尝试在其中以这种方式获取“foo”字符串:

String text1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ft2");

但我无法获得价值。 Text1 始终为空。我什至尝试将 ajax=false 设置为 commandButton,但没有任何改变。 知道我做错了什么吗?

【问题讨论】:

    标签: html forms jsf


    【解决方案1】:

    输入的name=value 对作为请求参数name=value 发送,而不是id=value。您需要改为设置name 属性。

    <input id="ft2" name="ft2" type="text" value="foo"/>
    

    与具体问题无关,我建议使用@ManagedProperty来设置值:

    @ManagedProperty("#{param.ft2}")
    private String ft2;
    

    【讨论】:

    • 第一件事:感谢您的解决方案。对于一般方法,我的问题更复杂。我不能使用 @ManagedProperty("#{param.ft2}") private String ft2;技术,因为我不知道我的页面上有多少输入字段:它们取决于用户可以自己创建的一些模板。出于同样的原因,我不能使用 inputHidden:首先我希望输入框对用户可见和可食用,其次我不知道我需要多少个。
    • 为这些输入提供所有相同的名称,并在String[] 属性上使用#{paramValues.ft}。它们将按照它们在 HTML DOM 中出现的顺序出现。抱歉 inputhidden,这是一个愚蠢的建议,我忽略了你没有使用type="hidden"
    猜你喜欢
    • 1970-01-01
    • 2015-02-28
    • 2012-01-27
    • 2011-01-23
    • 2015-03-11
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多