将文本字段值从 jsf 页面传递到托管 bean

Passing text field values from jsf page to managed bean

我有一个 JSF 表单,它在单击操作按钮时调用托管 bean 的方法。该方法已成功调用,但现在我想从 bean 访问在表单字段中输入的值。这是我的代码。

观点:

<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>

型号:

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

使用 JSF 时,您应该在表单上使用 JSF 字段,而不是使用:

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

您使用:

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

并且在您的托管 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 ") ;      
  }
}

您应该将一些属性放在支持 bean 中,例如:

private String enteredValue;

并为该属性添加 getter 和 setter。然后你应该将 value 属性添加到 <h:inputText value="#{backingbean.enteredValue}"

现在您可以从 "button click" 方法访问该值。

像这样使用 getter 和 setter 从 xhtml 中获取值

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

}

我知道这个问题没有提到 AJAX,但也许它对某人有帮助。

如前所述,

Java class 应该有 属性 和 setter/getter
但是 primeFeaces 提交按钮需要一个 process 属性。

<h:inputText id="firstNameElem" value="#{someController.firstName}" />
<p:commandButton
    value="Ok"
    type="submit"
    ajax="true"
    process="@this firstNameElem"
    action="#{someController.doThings}">
</p:commandButton>