简体   繁体   English

在Tapestry的BeanEditor中使用自定义类

[英]Using custom classes in Tapestry's BeanEditor

I have a class consisting of mostly String s and a few Double s which I have a BeanEditor for. 我有一个主要由String和几个Double组成的类,其中有一个BeanEditor

However, I wish to add another field to this class, and the type of that field is my custom class, let's call it Bar . 但是,我希望向该类添加另一个字段,并且该字段的类型是我的自定义类,我们将其称为Bar

public class Foo {
    private String myString;
    private Double myDouble;
    private Bar bar;

    // getters and setters
    ...
}

The allowed values for that field are pulled from a database and should appear as a select control. 该字段的允许值是从数据库中提取的,应显示为选择控件。

However, when I try to do this in my page, it's like the myBar doesn't exist in my Foo class. 但是,当我尝试在页面中执行此操作时,好像myBar在我的Foo类中不存在。

<beaneditor>
    <p:bar>
        <div class="t-beaneditor-row">
            <label>Bar</label>
            <t:select t:id="barSelecter" t:model="barModel" t:value="foo.bar" t:encoder="barEncoder"/>
        </div>
    </p:bar>
</beaneditor>

How do I accomplish the desired effect? 如何达到理想的效果?

You'll have to add the bar property to the model. 您必须将bar属性添加到模型中。

Eg <beaneditor add="bar"... > 例如<beaneditor add="bar"... >

You can write your own 'BeanEditor'-block for your type Bar . 您可以为Bar类型编写自己的'BeanEditor'块。

Like I did for my TourOperator: NamedDTOSelectTourOperatorBlocks.tml: 就像我为TourOperator所做的那样:NamedDTOSelectTourOperatorBlocks.tml:

<t:container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd" xmlns:p="tapestry:parameter">
     <t:block id="editSelectBar">

            <t:label for="tourOperatorEdit" />
              <t:select 
              value="prop:NamedDTOSelectTourOperatorEditContext.id" 
              t:model="tourOperatorIdsModel" t:validate="required" 
              t:id="tourOperatorEdit" t:blankOption="ALWAYS" 
              t:blankLabel="message:select.blank.label" 
              validate="required" />
        </t:block>
</t:container>

The NamedDTOSelectTourOperatorBlocks.java: NamedDTOSelectTourOperatorBlocks.java:

public class NamedDTOSelectTourOperatorBlocks {

    @Property
    @Environmental
    private PropertyOutputContext outputContext;

    @Property
    @Environmental
    private PropertyEditContext editContext;

    @Inject
    private ComponentResources resources;

    @Inject
    private TourOperatorManager tourOperatorManager;

    @Inject
    private SelectIdModelFactoryNamedDTO selectIdModelFactoryNamedDTO;

    private SelectModel tourOperatorIdsModel;

    void onPrepareForRender() {
        tourOperatorIdsModel = selectIdModelFactoryNamedDTO.create(tourOperatorManager.getAllNamedDTO());// Read the values from my DB
    }

    public SelectModel getTourOperatorIdsModel() {
        if (null == tourOperatorIdsModel) {
            // REMARK: I have no idea why but loading it in setupRender or onPrepareForRender seams to be too late.
            tourOperatorIdsModel = selectIdModelFactoryNamedDTO.create(tourOperatorManager.getAllNamedDTO());
        }
        return tourOperatorIdsModel;
    }

    public NamedDTOSelectTourOperator getNamedDTOSelectTourOperatorEditContext() {
        return (NamedDTOSelectTourOperator) editContext.getPropertyValue();
    }

    public NamedDTOSelectTourOperator getNamedDTOSelectTourOperatorOutputContext() {
        return (NamedDTOSelectTourOperator) outputContext.getPropertyValue();
    }
}

And don't forget to register the new defined Block in AdminModule.java : 并且不要忘记在AdminModule.java注册新定义的Block:

public static void contributeDefaultDataTypeAnalyzer(final MappedConfiguration<Class<?>, String> configuration) {
  ...
      configuration.add(NamedDTOSelectTourOperator.class, "NamedDTOSelectTourOperator");
...}


    public static void contributeDefaultDataTypeAnalyzer(final MappedConfiguration<Class<?>, String> configuration) {
...
     configuration.add(NamedDTOSelectTourOperator.class, "NamedDTOSelectTourOperator");
...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM