-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 ") ;      
  }
}
oldvipera
  • 305
  • 4
  • 8
  • 16
  • 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: http://stackoverflow.com/tags/jsf/info – BalusC Jan 27 '15 at 11:24
  • Turn plain HTML tags like `` 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 '15 at 11:26

4 Answers4

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());   
    }

}
Community
  • 1
  • 1
viper
  • 714
  • 1
  • 11
  • 26
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 ") ;      
  }
}
Mateus Viccari
  • 7,389
  • 14
  • 65
  • 101
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.

peterremec
  • 488
  • 1
  • 15
  • 46
  • did the same you said , gives a `value="#{submission.firstName}": Property 'firstName' not found on type com.main.MainClass` error – oldvipera Jan 27 '15 at 11:15
  • Can you post your added attribute and setter/getter? Also xhtml code update could help... – peterremec Jan 27 '15 at 11:20
  • i have updated my code here please check – oldvipera Jan 29 '15 at 08:53
  • I don't see any setter. Try to add one. In addition, make getter and setter public. – peterremec Jan 29 '15 at 09: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 '15 at 09:11
  • You're welcome. I'm quite new in JSF, so I can't explain how exactly does this work. I guess `setFirstName` setter is called when input is changed, since `h:inputText` component has `value="#{submission.firstName}"`. – peterremec Jan 29 '15 at 09:23
  • There is still a problem I am facing. The `h:inputText` takes value of `firstName` from the `getter` method. But when I use `getter` method after the page is redirected to another page, the value which is entered in the form doesn't come. – oldvipera Jan 29 '15 at 09:36
  • I'm not sure, but it could be scope of the managed bean. I guess `@ManagedBean` annotation makes 'request scope' bean by default. See: http://stackoverflow.com/questions/19322364/what-is-the-default-managed-bean-scope-in-a-jsf-2-application-in-netbeans – peterremec Jan 29 '15 at 09:46
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>
eglaf
  • 116
  • 1
  • 4
  • ??? 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 '17 at 10:19