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

如何通过Java类从Web服务动态填充ADF Form中的RichTable?

Hey there! Let's walk through how to make that "get Invoices" button pull data from your external web service and populate your RichTable in ADF. Since you already have your data control linked to the Java bean with all the table attributes, we can focus on the key implementation steps:

Step 1: Bind the Button to a Java Bean Method

First, connect your button to a custom method in your Java bean that will handle the service call and data population:

  • Open your ADF Faces page in the editor, select the "get Invoices" button.
  • In the Property Inspector, find the Action field, click the dropdown and select Edit....
  • In the Action Binding dialog, pick your Java bean data control, then create a new method (name it something like fetchAndPopulateInvoices()) and bind it to the button's action event.
Step 2: Implement the Service Call & Data Mapping Logic

Open your Java bean and add the core logic to fetch data, map it to your bean's entity class, and update the table's data source. Here's a concrete example (adjust based on your web service type—REST or SOAP):

import javax.faces.context.FacesContext;
import javax.faces.application.Application;
import javax.el.ExpressionFactory;
import javax.el.ELContext;
import oracle.adf.model.binding.DCIteratorBinding;
import java.util.List;
import java.util.ArrayList;

public class YourInvoiceBean {
    // Assume this is the list bound to your RichTable's data source
    private List<InvoiceEntity> invoiceList = new ArrayList<>();

    public void fetchAndPopulateInvoices() {
        try {
            // 1. Call the external web service to get raw invoice data
            List<ExternalInvoiceDto> externalInvoices = callExternalInvoiceService();

            // 2. Clear existing data to avoid duplicates
            invoiceList.clear();

            // 3. Map external data to your bean's entity class
            for (ExternalInvoiceDto extInvoice : externalInvoices) {
                InvoiceEntity invoice = new InvoiceEntity();
                invoice.setInvoiceId(extInvoice.getInvoiceId());
                invoice.setCustomerName(extInvoice.getCustomerName());
                invoice.setAmount(extInvoice.getTotalAmount());
                invoice.setInvoiceDate(extInvoice.getIssueDate());
                // Map all other required attributes here
                invoiceList.add(invoice);
            }

            // 4. Refresh the ADF iterator to trigger table update
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            ExpressionFactory elFactory = app.getExpressionFactory();
            ELContext elContext = context.getELContext();

            // Replace "YourInvoiceIterator" with your actual iterator name from the data control
            DCIteratorBinding iterator = (DCIteratorBinding) elFactory
                .createValueExpression(elContext, "#{bindings.YourInvoiceIterator}", DCIteratorBinding.class)
                .getValue(elContext);
            iterator.refresh();

        } catch (Exception e) {
            // Show error message to users
            FacesContext.getCurrentInstance().addMessage(null, 
                new javax.faces.application.FacesMessage(
                    javax.faces.application.FacesMessage.SEVERITY_ERROR,
                    "Failed to fetch invoices",
                    "Error connecting to external service: " + e.getMessage()
                )
            );
            e.printStackTrace();
        }
    }

    // Helper method to call external service (adjust for REST/SOAP)
    private List<ExternalInvoiceDto> callExternalInvoiceService() throws Exception {
        List<ExternalInvoiceDto> invoices = new ArrayList<>();
        // Example REST call (replace with your service logic):
        // URL url = new URL("https://your-service-url.com/api/invoices");
        // HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        // conn.setRequestMethod("GET");
        // Read response stream and parse into ExternalInvoiceDto objects
        return invoices;
    }

    // Getter for the invoice list (already linked to your data control)
    public List<InvoiceEntity> getInvoiceList() {
        return invoiceList;
    }
}
Step 3: Verify Table Binding

Double-check your RichTable's configuration to ensure it's wired correctly:

  • The table's value property should be bound to your data control's collection, e.g., #{bindings.YourInvoiceList.collectionModel}.
  • Each table column should map to your bean's attributes, like #{row.invoiceId} or #{row.amount}.
Step 4: Test & Troubleshoot

Deploy your app and test the button. If data doesn't show up, check these common issues:

  • Is the external service returning valid data? Add debug logs to confirm the callExternalInvoiceService() method works.
  • Is the invoiceList being updated correctly? Inspect the list after mapping.
  • Did the iterator refresh trigger? Ensure your iterator name matches the one in the code.
  • Check browser dev tools or ADF server logs for any errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:18:44