Tapestry 5.2 在哪里定义类?

发布于 2024-09-09 05:16:04 字数 1947 浏览 2 评论 0原文

我有一个用于网格中每一行的类,它是多个实体数据的混合。问题是把它放在哪里?我尝试将该类放置在组件包中并在我的页面中实例化它,尝试从注入的服务返回该类,并将该类嵌套在页面 POJO 中。在所有情况下,它都会工作一次,然后在刷新时我会收到此错误:

Failure writing parameter 'row' of component admin/Add:grid.rows: Failure writing parameter 'row' of component admin/Add:grid: Could not find a coercion from type com.foo.bar.pages.admin.Add$RolesRow to type com.foo.bar.pages.admin.Add$RolesRow.

它是我能想到的无用错误。我知道 Tapestry 希望您注入所有依赖项,但我还没有弄清楚使用什么模式来注入您要动态实例化的类。

类:

   public static class RolesRow{
    @Property public Boolean ROLE_SUPER;
    @Property public Boolean ROLE_ADMIN;
    @Property public Boolean ROLE_USER;
    @Property public Boolean ROLE_VIEW;
    @Property public Boolean ROLE_EMAIL;
    private Map<String,Role> roles;
    public RolesRow(){

    }
    public RolesRow(Map<String,Role> roles){
        ROLE_SUPER = (roles.containsKey("ROLE_SUPER") ? true:false);
        ROLE_ADMIN = (roles.containsKey("ROLE_ADMIN") ? true:false);
        ROLE_USER =  (roles.containsKey("ROLE_USER") ? true:false);
        ROLE_VIEW =  (roles.containsKey("ROLE_VIEW") ? true:false);
        ROLE_EMAIL = (roles.containsKey("ROLE_EMAIL") ? true:false);
        this.roles = roles;
    }
    public List<Role> selected(){
        List<Role> r = new ArrayList<Role>();
        checked(r,"ROLE_SUPER",ROLE_SUPER);
        checked(r,"ROLE_ADMIN",ROLE_ADMIN);
        checked(r,"ROLE_USER",ROLE_USER);
        checked(r,"ROLE_VIEW",ROLE_VIEW);
        checked(r,"ROLE_EMAIL",ROLE_EMAIL);
        return r;
    }
    private void checked(List<Role> r,String field,Boolean obj){
        if (obj !=null && obj == true){
            r.add(roles.get(field));
        }

    }

编辑:这是我在 setupRender 中实例化记录的

        void setupRender(){
         List<RolesRow> list = new ArrayList<RolesRow>();
     list.add(new RolesRow());

I have a class that I am using for each row in a grid, it is the mix of multiple entities data. the question is where to put it? I tried placing the class in the component package and instantiating it within my page, tried returning the class from an injected service, and nesting the class within the page POJO. in all cases it will work once and then on refresh I get this error:

Failure writing parameter 'row' of component admin/Add:grid.rows: Failure writing parameter 'row' of component admin/Add:grid: Could not find a coercion from type com.foo.bar.pages.admin.Add$RolesRow to type com.foo.bar.pages.admin.Add$RolesRow.

its about as non helpful an error that I can think of. I get that Tapestry wants you to inject all dependencies but I haven't figured what pattern to use to inject a class that you are going to instantiate on the fly.

Edit: here is the class

   public static class RolesRow{
    @Property public Boolean ROLE_SUPER;
    @Property public Boolean ROLE_ADMIN;
    @Property public Boolean ROLE_USER;
    @Property public Boolean ROLE_VIEW;
    @Property public Boolean ROLE_EMAIL;
    private Map<String,Role> roles;
    public RolesRow(){

    }
    public RolesRow(Map<String,Role> roles){
        ROLE_SUPER = (roles.containsKey("ROLE_SUPER") ? true:false);
        ROLE_ADMIN = (roles.containsKey("ROLE_ADMIN") ? true:false);
        ROLE_USER =  (roles.containsKey("ROLE_USER") ? true:false);
        ROLE_VIEW =  (roles.containsKey("ROLE_VIEW") ? true:false);
        ROLE_EMAIL = (roles.containsKey("ROLE_EMAIL") ? true:false);
        this.roles = roles;
    }
    public List<Role> selected(){
        List<Role> r = new ArrayList<Role>();
        checked(r,"ROLE_SUPER",ROLE_SUPER);
        checked(r,"ROLE_ADMIN",ROLE_ADMIN);
        checked(r,"ROLE_USER",ROLE_USER);
        checked(r,"ROLE_VIEW",ROLE_VIEW);
        checked(r,"ROLE_EMAIL",ROLE_EMAIL);
        return r;
    }
    private void checked(List<Role> r,String field,Boolean obj){
        if (obj !=null && obj == true){
            r.add(roles.get(field));
        }

    }

I am instantiating the record in setupRender:

        void setupRender(){
         List<RolesRow> list = new ArrayList<RolesRow>();
     list.add(new RolesRow());

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

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

发布评论

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

评论(1

仅此而已 2024-09-16 05:16:04

类似的业务类不应位于 Tapestry 的“神奇”文件夹内(componentsmixinspagesservices)。

每当包中的任何文件发生更改时,Tapestry 都会重新加载(并重新转换)该类。 (这是实时类重新加载机制的一部分。)当您在会话中存储该类的对象时,会导致奇怪的类转换异常,例如“A 无法转换为 A”。

作为一般规则,仅将属于 Tapestry 组件的类放在 components 文件夹中,并将其他所有内容放在其他位置。当您有一个确实应该是组件类(例如您的类)的内部类的类时,这可能会很痛苦,因为您必须将其移到使用它的组件之外。

您也将无法使用 Tapestry 的元编程魔法,例如 @Property

Business classes like that should not be located inside Tapestry's "magic" folders (components, mixins, pages, orservices).

Tapestry will reload (and re-transform) the class whenever any file in its package changes. (This is part of the live class reloading mechanism.) When you store objects of that class in the session, that results in weird class cast exceptions, such as "A cannot be cast to A."

As a general rule, only put classes in the components folder that are Tapestry components, and put everything else elsewhere. This can be a pain when you have a class that really should be an inner class of a component class, such as yours, as you will have to move it outside the component where it is used.

You will also not be able to use Tapestry's metaprogramming magic, such as @Property.

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