h:dataTable 里面的 h:commandButton [英] h:commandButton inside h:dataTable

查看:22
本文介绍了h:dataTable 里面的 h:commandButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 JSF 数据表.表格中的一列是命令按钮.

I am using a JSF data table. One of the columns in the table is a Command button.

单击此按钮时,我需要使用表达式语言传递一些参数(例如所选行的值).需要将这些参数传递给 JSF 托管 bean,该 bean 可以对其执行方法.

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.

我使用了以下代码片段,但我在 JSF bean 上获得的值始终为空.

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 方法:

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?

How can I pass values to the JSF managed bean with a commandbutton?

推荐答案

使用 DataModel#getRowData() 在 action 方法中获取当前行.

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();
        doctorModel = new ListDataModel<Doctor>(doctors);
    }

    public void followDoctor() {
        Doctor selectedDoctor = doctorModel.getRowData();
        // ...
    }

    // ...
}

改为在数据表中使用它.

Use it in the datatable instead.

<h:dataTable value="#{usermanager.doctorModel}" var="doc">

并去掉视图中 h:commandButton 旁边的 h:inputHidden.

And get rid of that h:inputHidden next to the h:commandButton in the view.

一个不太优雅的替代方法是使用 f:setPropertyActionListener.

An -less elegant- alternative is to use f:setPropertyActionListener.

public class Usermanager {
    private Long doctorId;

    public void followDoctor() {
        Doctor selectedDoctor = getItSomehowBy(doctorId);
        // ...
    }

    // ...
}

使用以下按钮:

<h:commandButton action="#{usermanager.followDoctor}" value="Follow">
    <f:setPropertyActionListener target="#{usermanager.doctorId}" value="#{doc.doctorId}" />
</h:commandButton>


相关:

  • @的好处和陷阱ViewScoped - 包含使用 DataModel 的 CRUD 示例.

  • Related:

    • The benefits and pitfalls of @ViewScoped - Contains CRUD example using DataModel<E>.
    • 这篇关于h:dataTable 里面的 h:commandButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆