我有一个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 ") ;
}
}
使用 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());
}
}
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}"
现在,您可以通过"按钮单击"方法访问该值。
我知道这个问题没有提到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>