问题描述
我正在使用一个JSF数据表。表中的列之一是命令按钮。当单击此按钮时,我需要传递几个参数(如所选行的值),使用表达式语言。这个参数需要传递给可以在其上执行方法的JSF托管bean。
我使用了以下代码片段,但是我在JSF bean总是为空。
< h:column>
< f:facet name =header>
< h:outputText value =跟随/>
< / f:facet>
< h:commandButton id =FollwDocaction =#{usermanager.followDoctor}value =跟随/>
< h:inputHidden id =id1value =#{doc.doctorid}/>
< / h:column>
Bean方法:
public void followDoctor(){
FacesContext context = FacesContext.getCurrentInstance();
Map requestMap = context.getExternalContext()。getRequestParameterMap();
String value =(String)requestMap.get(id1);
System.out.println(Doctor added to patient List+ value);
}
如何使用命令按钮将值传递给JSF托管bean? p>
使用以获取当前行的操作方法。
@ManagedBean
@ViewScoped
public class Usermanager {
private List& Doctor>医生;
private DataModel& Doctor>医生模型
@PostConstruct
public void init(){
doctors = getItSomehow();
datamodel = new ListDataModel& Doctor>(医生);
}
public void followDoctor(){
Doctor selectedDoctor = doctorModel.getRowData();
// ...
}
// ...
}
在datatable中使用它。
< h:dataTable value = #{usermanager.doctorModel}var =doc>
并摆脱那个 h:inputHidden
在视图中 h:commandButton
旁边。
没有优雅的选择是使用 f:setPropertyActionListener
。
public Usermanager类{
private Long doctorId;
public void followDoctor(){
Doctor selectedDoctor = getItSomehowBy(doctorId);
// ...
}
// ...
}
使用以下按钮:
< h:commandButton action =# usermanager.followDoctor}value =跟随>
< f:setPropertyActionListener target =#{usermanager.doctorId}value =#{doc.doctorId}/>
< / h:commandButton>
相关:
- 的好处和陷阱 - 包含使用
DataModel< E>
的CRUD示例。 li>
I am using a JSF data table. One of the columns in the table is a Command button.
When this button is clicked I need to pass few parameters (like a value of the selected row) using the Expression language. This paramaters need to be passed to the JSF managed bean which can execute methods on them.
I have used the following snippet of code but the value i am getting on the JSF bean is always null.
<h:column>
<f:facet name="header">
<h:outputText value="Follow"/>
</f:facet>
<h:commandButton id="FollwDoc" action="#{usermanager.followDoctor}" value="Follow" />
<h:inputHidden id="id1" value="#{doc.doctorid}" />
</h:column>
Bean Method:
public void followDoctor() {
FacesContext context = FacesContext.getCurrentInstance();
Map requestMap = context.getExternalContext().getRequestParameterMap();
String value = (String)requestMap.get("id1");
System.out.println("Doctor Added to patient List"+ value);
}
How can I pass values to the JSF managed bean with a commandbutton?
Use DataModel#getRowData()
to obtain the current row in action method.
@ManagedBean
@ViewScoped
public class Usermanager {
private List<Doctor> doctors;
private DataModel<Doctor> doctorModel;
@PostConstruct
public void init() {
doctors = getItSomehow();
datamodel = new ListDataModel<Doctor>(doctors);
}
public void followDoctor() {
Doctor selectedDoctor = doctorModel.getRowData();
// ...
}
// ...
}
Use it in the datatable instead.
<h:dataTable value="#{usermanager.doctorModel}" var="doc">
And get rid of that h:inputHidden
next to the h:commandButton
in the view.
An -less elegant- alternative is to use f:setPropertyActionListener
.
public class Usermanager {
private Long doctorId;
public void followDoctor() {
Doctor selectedDoctor = getItSomehowBy(doctorId);
// ...
}
// ...
}
With the following button:
<h:commandButton action="#{usermanager.followDoctor}" value="Follow">
<f:setPropertyActionListener target="#{usermanager.doctorId}" value="#{doc.doctorId}" />
</h:commandButton>
Related:
- The benefits and pitfalls of
@ViewScoped
- Contains CRUD example usingDataModel<E>
.
这篇关于h:commandButton里面的h:dataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!