user1504940
user1504940

Reputation: 175

how to pass input text value as method parameter from JSF page to Managedbean?

I want to pass input text value as method parameter from jsf page to managedbean as i submit the form. like

<h:form>
   <p:inputText name="fname"/>
   <P:commandButton value="Submit" action="#{myClass.save(inputText value as parameters)}">
</h:form>

Upvotes: 2

Views: 5892

Answers (1)

perissf
perissf

Reputation: 16273

The standard approach is the following:

<h:form> 
    <p:inputText value="#{myClass.inputValue}"/> 
    <p:commandButton value="Submit" action="#{myClass.doSomething}"> 
</h:form> 

MyClass class:

@ManagedBean
@ViewScoped
public class myClass {
    private String inputValue;

    // getter and setter for inputValue

    public void doSomething() {
    }
}

When the user clicks the button, before doSomething is invoked, JSF cares about reading the value inputed in the inputText component, validating it if necessary, and invoking setter class for inputValue as per the EL expression #{myClass.inputValue} (note that EL automagically understands that the setter must be invoked in this case). This is very basic JSF. I recommend to study some Java EE 6 tutorial (this, for example).

Upvotes: 1

Related Questions