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

时间:2015-01-27 10:45:51

标签: jsf 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 ") ;      
  }
}

4 个答案:

答案 0 :(得分:1)

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

}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您应该在支持bean中添加一些属性,如:

private String enteredValue;

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

现在,您可以通过“&#34;”按钮点击&#34;方法

答案 3 :(得分:0)

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

Java类应该具有前面提到的带有setter / getter 的属性 但是primeFeaces提交按钮需要进程属性。

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