Tapestry 5 - 两个组件之间的通信

发布于 2024-11-25 05:57:19 字数 1127 浏览 1 评论 0原文

我的 Java + Tapestry 5 应用程序中有一个页面,其中包含两个组件 - 表单和网格。表单字段用于过滤网格中显示的结果。该表单只是一个文本字段,用于设置它所映射到的对象 A 的值。网格是从对象 A 创建的。我需要将正确的实例从表单组件发送到网格组件。最好的方法是什么?我可以通过在上页中设置对象来使用普通的旧 java 方式来完成此操作,但应该有一种更干净的方式。我正在考虑环境注释,但这不是太重了吗?

public class I1 {
    @Component
    private WFRFormFilter wfrFormFilter;

    @Component (parameters={ "wfrDataHolder=property:wfrFormFilter.wfrDataHolder" })
    private WFRResultTable wfrResultTable;
}

public class WFRFormFilter {
    @Inject
    private WFRService wfrservice;

    @Propperty
    @Persist
    private WFRDataHolder wfrDataHolder;

    @PageAttached
    void pageAttached() {
        if (wfrDataHolder == null) { 
            wfrDataHolder = new WFRDataHolder(); 
        }
    }

    @OnEvent(EventConstants.SUCCESS)
        void processMyForm() { }
}

public class WFRResultTable {
    @Parameter(defaultPrefix=BindingConstants.PROP)
    @Property
    private WFRDataHolder wfrDataHolder;

    @Inject
    private WFRService wfrservice;

    public List<WFRDataHolder> getResultTableData() {
        return wfrservice.getData(this.wfrDataHolder);
    }   
}

I have a page in my Java + Tapestry 5 application, which contains two components - a Form and a Grid. The form field is use to filter the results displayed in the grid. The form is just a textfield which sets the value of the object A on which it is mapped. The grid is created from the object A. I need to send the right instance to the grid component from the form component. What is the best way to do this? I could do it the plain old java way by setting the object in the upper page, but there should be an cleaner way. I was thinking about environment annotation, but isn't that too heavy?

public class I1 {
    @Component
    private WFRFormFilter wfrFormFilter;

    @Component (parameters={ "wfrDataHolder=property:wfrFormFilter.wfrDataHolder" })
    private WFRResultTable wfrResultTable;
}

public class WFRFormFilter {
    @Inject
    private WFRService wfrservice;

    @Propperty
    @Persist
    private WFRDataHolder wfrDataHolder;

    @PageAttached
    void pageAttached() {
        if (wfrDataHolder == null) { 
            wfrDataHolder = new WFRDataHolder(); 
        }
    }

    @OnEvent(EventConstants.SUCCESS)
        void processMyForm() { }
}

public class WFRResultTable {
    @Parameter(defaultPrefix=BindingConstants.PROP)
    @Property
    private WFRDataHolder wfrDataHolder;

    @Inject
    private WFRService wfrservice;

    public List<WFRDataHolder> getResultTableData() {
        return wfrservice.getData(this.wfrDataHolder);
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2024-12-02 05:57:19

Tapestry 的参数是双向工作的:如果您将数据存储在页面类中(使用 @Persist)并将其作为参数传递给表单组件,则可以在表单组件中更改其值,并且这将自动反映到您的页面:无需调用设置器。

因此,如果您的页面和表单组件的布局如下:

 public class MyPage {

     @Component(parameters = "dataHolder=dataHolder")
     private MyForm formComponent;

     @Component(parameters = "dataHolder=dataHolder")
     private MyGrid gridComponent;

     @Persist
     @Property
     private MyDataHolder dataHolder;

}

public class MyFormComponent {

    @Parameter
    private MyDataHolder dataHolder;

    @OnEvent(EventConstants.SUCCESS)
    void formSubmitted() {
        this.dataHolder = new DataHolder(...);
    }

}

...那么正确的值将始终出现在页面中,因此也出现在网格组件上。

另请参阅 Tapestry 文档中的 组件参数页面,特别是“参数是双向的”部分定向”。

Tapestry's parameters work bi-directionally: If you store your data in your page class (with @Persist) and pass it to your form component as a parameter, you can change its value in the form component, and that will be reflected to you page automatically: no calls to setters required.

So if you have your page and form component laid out like this:

 public class MyPage {

     @Component(parameters = "dataHolder=dataHolder")
     private MyForm formComponent;

     @Component(parameters = "dataHolder=dataHolder")
     private MyGrid gridComponent;

     @Persist
     @Property
     private MyDataHolder dataHolder;

}

public class MyFormComponent {

    @Parameter
    private MyDataHolder dataHolder;

    @OnEvent(EventConstants.SUCCESS)
    void formSubmitted() {
        this.dataHolder = new DataHolder(...);
    }

}

...then the proper value will always be present in the page, and thus also on the grid component.

Also see the Component Parameters page in the Tapestry docs, particularly the section "Parameters are Bi-Directional".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文