0

I have read a lot of posts at Stackoverflow but I didn't succeed in implementing the belowmentioned problem from my side.

the problem is: I need to type some text in <p:inputTextarea> and when clicking on button I need to get this value in the bean method.

I.e.:

<p:inputTextarea binding="#{input}"/>
<p:commandButton action="#{pessoaMB.adicionarContato(input.value)}" immediate="true"/>

with the bean method:

public void adicionarContato(String value) {
    System.out.println(value);
}

The code I'm using gives me a null value.

I'm using @ViewScoped and cannot change this.

skuntsel
  • 11,624
  • 11
  • 44
  • 67
Al2x
  • 1,001
  • 5
  • 26
  • 37
  • 1
    Related: http://stackoverflow.com/questions/12506679/what-is-component-binding-in-jsf-when-it-is-preferred-to-be-used – BalusC Jun 21 '13 at 01:19

3 Answers3

1

First of all, a side note: it is a bad practice to work with JSF components, you should work with model instead. I.e. don't use binding="#{input}", but stick to value="#{bean.text}".

Second, I doubt that immediate="true" is used appropriately in your setup. When used in a UICommand component like <h:commandButton> it will cause to skip JSF lifecycle for components with immediate="false" (or omitted, as it's the default), thus their value won't be set at all. Still, JSF will still preset submittedValue behind the scenes before the action method is executed.

Also, I strongly recommend to read BalusC's blog post Debug JSF lifecycle, as it is more than enlightening on the topic.

As to the solution, I'd suggest to deal with value binding with the bean, as presented in the first comment. With this approach you won't need action method parameter at all. Moreover, rethink your use of immediate attribute. If you think it's correct then you've got two choices: (1) use immediate="true" on <p:inputTextarea> or (2) switch to action="#{bean.action(input.submittedValue)}".

skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • **RESOLVED** Thanks for all posted topics about this issue. You @skuntsel are totally right. Ive missed this information about lifecycle immediate.... Thats resolved. thank you. – Al2x Jun 21 '13 at 12:44
0

I would've done this :

<h:form>
 <p:inputText value="#{pessoaMB.input}"/>
 <p:commandButton value="add" action="#{pessoaMB.adicionarContato}" /> 
</h:form>

input would be here a pessoaMB property with a getter and setter (an IDE can autogenerate it).

private String input;

public String getInput() {
    return input;
}

public void setInput(String input) {
    this.input = input;
}

As for the adicionarContato method, it would be like this :

public void adicionarContato() {
    System.out.println(input);
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Akheloes
  • 1,352
  • 3
  • 11
  • 28
  • thats right. But I have about 10 inputText in the form which I need to type some values and clicking on the button I need to add them to the dataTable row. Do i need to create about 10 String properties in the bean to do this ? Thats the only way @Gloserio ? thanks – Al2x Jun 21 '13 at 11:52
  • @BalusC if possible Id like your opinion about this question. thanks in advance. – Al2x Jun 21 '13 at 13:18
0

You should create a new class, i.e:

public class MyFields(){
    String input1;
    String input2; //and so on...
    //getters and setters
}

Then, in pessoaMB create a property:

private MyFields inputFields; //getter and setter

Finally, in your xhtml file:

<h:form>
  <p:inputText value="#{pessoaMB.inputFields.input1}"/>
  <p:inputText value="#{pessoaMB.inputFields.input2}"/>
  <!-- add more inputText components... -->
  <p:commandButton value="add" action="#{pessoaMB.adicionarContato}" /> 
</h:form>
danRod
  • 113
  • 5