themarcuz
themarcuz

Reputation: 2583

JSF reading dynamic input element in a managed bean

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>

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");

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?

Upvotes: 3

Views: 1412

Answers (1)

BalusC
BalusC

Reputation: 1108722

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

Unrelated to the concrete problem, I suggest to use @ManagedProperty instead to set the value:

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

Upvotes: 4

Related Questions