JSF读取托管bean中的动态输入元素 [英] JSF reading dynamic input element in a managed bean
问题描述
我有一个非常复杂的JSF页面(我们使用带facelet的JSF2),其中我必须插入一个纯html表单部分(它表示一个WYSIWYG模板,用于稍后将创建为PDF的文档)。
非常简化的页面看起来像
I have a pretty complex JSF page (we use JSF2 with facelet) in which I have to "plug-in" a pure html form section (it represents a WYSIWYG template for an document that will be created as Pdf later). Very simplified the page looks like
<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字符串:
In the managed bean myBean (request scoped) I have the action listener in which I try to get the "foo" string in this way:
String text1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("ft2");
但我无法获得价值。 Text1始终为空。我甚至试图设置ajax = false到commandButton,但没有任何改变。
关于我在做什么错误的任何想法?
But I can't get the value. Text1 is always null. I even tried to set ajax=false to the commandButton, but nothing changed. Any idea about what I am doing wrong?
推荐答案
这是输入的 name = value
一对作为请求参数发送 name = value
,而不是 id = value
。您需要改为设置名称
属性。
It's the input's name=value
pair which get sent as request parameter name=value
, not the id=value
. You need to set the name
attribute instead.
<input id="ft2" name="ft2" type="text" value="foo"/>
无关具体问题,我建议使用 @ManagedProperty
来设置值:
Unrelated to the concrete problem, I suggest to use @ManagedProperty
instead to set the value:
@ManagedProperty("#{param.ft2}")
private String ft2;
这篇关于JSF读取托管bean中的动态输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!