Passing text field values from jsf page to managed bean [duplicate]

倖福魔咒の 提交于 2019-12-23 06:38:49

问题


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

回答1:


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

}



回答2:


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



回答3:


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.




回答4:


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>


来源:https://stackoverflow.com/questions/28168573/passing-text-field-values-from-jsf-page-to-managed-bean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!