-1

I have a JSF form which calls a method of a managed bean when the action button is clicked. The method is successfully called, but now I would like to access the values entered in the form field from the bean. Here is my code.

The view:

<h:form class="form-horizontal" action= "#{hello_World.message}" method="post" id="formId">      
 <div class="control-group">
  <label class="control-label" for="inputEmail">First Name</label>
  <div class="controls">
  <h:inputText id="firstname" placeholder="First Name" value="#{submission.firstName> </h:inputText>
  </div> 

  <div class="control-group">
   <label class="control-Label">Address</label>
   <div class="controls">
   <input type="text" placeholder="Address" />
   </div>
   </div>
    <h:commandButton value="click" action="#{submission.submitted}"/>       
</h:form>

The model:

@ManagedBean(name="submission", eager=true)
public class MainClass {    
String firstName = "Pranbsh";
public MainClass(){
    System.out.println("Helloworld started from managed bean");
}
private String getFirstName(){
    return firstName;
}
public void submitted(){
    System.out.println("Bean executed");
    System.out.println("First name is ") ;      
  }
}
2
  • I strongly recommend to take a pause and work through a sane JSF book first. This is namely already answered in chapter 1 of such a book and in every other sane "Hello World", such as the one here: stackoverflow.com/tags/jsf/info
    – BalusC
    Jan 27, 2015 at 11:24
  • Turn plain HTML tags like <input type="text" placeholder="Address" /> into corresponding JSF specific tags and use value binding expressions to set values entered into the input fields to a managed bean in question.
    – Tiny
    Jan 27, 2015 at 11:26

4 Answers 4

3

Use getter and setter to get the values from the xhtml like this

JSF form

<h:form class="form-horizontal" action= "#{hello_World.message}" method="post" id="formId">      
  <div class="control-group">
  <label class="control-label" for="inputEmail">First Name</label>
  <div class="controls">
  <h:inputText id="firstname" placeholder="First Name" value="#{submission.firstName> </h:inputText>
  </div> 

   <div class="control-group">
   <label class="control-Label">Address</label>
   <div class="controls">
   <input type="text" placeholder="Address" />
   </div>
   </div>
    <h:commandButton value="click" action="#{submission.submitted}"/>       
</h:form>

Managed Bean

  public class Form {
    String firstName ="Pranish";

    public String getFirstName(){
        return firstName;
    }

    public void setFirstName(String firstName){
        this.firstName = firstName;
    }

    public void submitted(){
        System.out.println("Bean executed");
        setFirstName(firstName);
        System.out.println("First Name : " + getFirstName());   
    }

}
0

When working with JSF, you should use JSF fields on your forms, so instead of using:

<input type="text" placeholder="Address" />

You use:

<h:inputText placeholder="Address" value="#{submission.address}"/>

And in your managed bean:

@ManagedBean(name="submission", eager=true)
public class MainClass {  

private String address; //+ getters and setters

public MainClass(){
    System.out.println("Helloworld started from managed bean");
}

public void submitted(){
    System.out.println("Bean executed");
    System.out.println("First name is ") ;      
  }
}
0

You should put some attribute in a backing bean like:

private String enteredValue;

and add getter and setter for that attribute. Then you should add value attribute into like <h:inputText value="#{backingbean.enteredValue}"

Now you can access that value from your "button click" method.

8
  • did the same you said , gives a value="#{submission.firstName}": Property 'firstName' not found on type com.main.MainClass error
    – oldvipera
    Jan 27, 2015 at 11:15
  • Can you post your added attribute and setter/getter? Also xhtml code update could help...
    – peterremec
    Jan 27, 2015 at 11:20
  • i have updated my code here please check
    – oldvipera
    Jan 29, 2015 at 8:53
  • I don't see any setter. Try to add one. In addition, make getter and setter public.
    – peterremec
    Jan 29, 2015 at 9:07
  • Made getter public and it worked. Thank you. But I still don't understand how the xhtml automatically detects the getter method of the firstName variable
    – oldvipera
    Jan 29, 2015 at 9:11
0

I know the question haven't mentioned AJAX, but maybe it helps someone.

Java class should have the property with setter/getter as it was mentioned before.
But the primeFeaces submit button needs a process attribute.

<h:inputText id="firstNameElem" value="#{someController.firstName}" />
<p:commandButton
    value="Ok"
    type="submit"
    ajax="true"
    process="@this firstNameElem"
    action="#{someController.doThings}">
</p:commandButton>
1
  • ??? If you omit the whole process attribute it works fine too. There is no need for the process attribute, it is an optimization, normally to exclude sending to much data (usually @this instead of using it to explicitly send more). But if you added @this then yes, the inputText is not send unless you add its id to the process attributte OR (and that is usually done) send it to the server via ajax inside the inputText
    – Kukeltje
    Nov 28, 2017 at 10:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.