You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从只读MAF表单布局获取taskId并传递至Java类?

解决方案:传递taskId到Managed Bean的ActionListener

要解决这个问题,你有两种简单的方式可以把taskId的值传递给你的closeTask bean的click方法,下面分别说明:

方法1:使用<amx:setPropertyListener>提前设置Bean属性

这种方式是先把taskId的值绑定到closeTask bean的一个属性上,然后在click方法里直接使用这个属性:

步骤1:在你的closeTask Managed Bean中添加属性和对应的getter/setter

private String taskId; // 根据实际数据类型调整,比如Long、Integer等

public String getTaskId() {
    return taskId;
}

public void setTaskId(String taskId) {
    this.taskId = taskId;
}

public void click(ActionEvent actionEvent) {
    // 现在可以直接使用this.taskId执行删除逻辑
    System.out.println("待删除的Task ID: " + taskId);
    // 这里编写你的删除业务代码...
}

步骤2:修改AMX页面中的Yes按钮,添加setPropertyListener

把原来的Yes按钮代码修改为:

<amx:commandButton text="Yes" id="cb5" actionListener="#{closeTask.click}">
    <amx:setPropertyListener from="#{bindings.taskId.inputValue}" 
                             to="#{closeTask.taskId}" 
                             type="action"/>
</amx:commandButton>

当用户点击Yes按钮时,taskId的值会先被设置到closeTask bean的属性中,随后执行click方法,方法里就能直接拿到这个值了。

方法2:在ActionListener方法中直接获取绑定值

如果你不想额外添加属性,也可以在click方法里直接通过ADF绑定层获取taskId的值:

修改你的closeTask bean的click方法:

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.jbo.Row;
import javax.faces.event.ActionEvent;

public void click(ActionEvent actionEvent) {
    // 获取绑定容器
    DCBindingContainer bindingContainer = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
    // 替换成你页面中对应的迭代器名称
    DCIteratorBinding iterator = bindingContainer.findIteratorBinding("YourIteratorName"); 
    Row currentRow = iterator.getCurrentRow();
    if (currentRow != null) {
        // 替换成实体对象中对应的属性名
        Object taskId = currentRow.getAttribute("taskId"); 
        System.out.println("待删除的Task ID: " + taskId);
        // 这里编写你的删除业务代码...
    }
}

这种方式不需要修改AMX页面的按钮代码,直接在Java方法里从绑定层获取当前行的taskId值。

完整修改后的AMX页面代码(使用方法1的版本)

<?xml version="1.0" encoding="UTF-8" ?>
<amx:view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:amx="http://xmlns.oracle.com/adf/mf/amx" 
          xmlns:dvtm="http://xmlns.oracle.com/adf/mf/amx/dvt">
    <amx:panelPage id="pp1" >
        <amx:facet name="header">
            <amx:outputText value="Task Details" id="ot1"/>
        </amx:facet>
        <amx:facet name="primary">
            <amx:commandButton id="cb1" action="__back"/>
        </amx:facet>
        <amx:facet name="secondary">
            <amx:commandButton id="cb2"/>
        </amx:facet>
        <amx:validationGroup id="group2">
            <amx:panelFormLayout id="pfl1">
                <amx:panelLabelAndMessage label="#{bindings.taskId.hints.label}" id="plam2">
                    <amx:outputText value="#{bindings.taskId.inputValue}" id="ot3" rendered="#{bindings.taskId}"/>
                </amx:panelLabelAndMessage>
                <amx:panelLabelAndMessage label="#{bindings.taskType.hints.label}" id="plam1">
                    <amx:outputText value="#{bindings.taskType.inputValue}" id="ot2"/>
                </amx:panelLabelAndMessage>
                <amx:panelLabelAndMessage label="#{bindings.taskName.hints.label}" id="plam3">
                    <amx:outputText value="#{bindings.taskName.inputValue}" id="ot5"/>
                </amx:panelLabelAndMessage>
            </amx:panelFormLayout>
        </amx:validationGroup>
        <amx:commandButton text="DELETE" id="cb3" inlineStyle="left:150px;" >
            <amx:validationBehavior id="validationBehavior2" group="group2"/>
            <amx:showPopupBehavior popupId="popup1" align="topStart" alignId="pp1" type="action" decoration="anchor" id="spb1"/>
        </amx:commandButton>
    </amx:panelPage>
    <amx:popup id="popup1" animation="slideUp" backgroundDimming="on" autoDismiss="true">
        <amx:commandButton text="No" id="cb4" action="__back"/>
        <amx:outputText value="Are you sure?" id="ot4"/>
        <amx:commandButton text="Yes" id="cb5" actionListener="#{closeTask.click}">
            <amx:setPropertyListener from="#{bindings.taskId.inputValue}" 
                                     to="#{closeTask.taskId}" 
                                     type="action"/>
        </amx:commandButton>
    </amx:popup>
</amx:view>

注意:如果taskId的类型不是String,记得调整Bean中属性的类型,确保和绑定值的类型匹配。

内容的提问来源于stack exchange,提问作者Pulkit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:01:36