Search code examples
javatapestry

inheritance design to composition design in tapestry5


how can I transform the next example from inheritance design to composition design in tapestry 5?

ParentComponent.tml:

<container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter" t:content="text/html; charset=utf-8">
    this is parent component<br/>

    <extension-point id="body_child"/>
</container>

ParentComponent.java:

public abstract class ParentComponent {

@Property
@Parameter(required=true)
private String param1;
@Property
@Parameter(required=true)
private String param2;
@Property
@Parameter(required=true)
private String param3;

}

C1.tml:

<t:extend xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter" t:content="text/html; charset=utf-8">
<t:replace id="body_child">
    body of c1 ${param1}, ${param2}, ${param3}
</t:replace>

C1.java:

public class C1 extends ParentComponent {

}

test.tml:

<t:c1 param1="1" param2="2" param3="3"/>

Thanks in advance.


Solution

  • It sounds like you are just looking for a layout component. Which you can achieve like so:

    Add a body to your layout component (parent)

    <container xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter" t:content="text/html; charset=utf-8">
        this is parent component<br/>
        <t:body/>
    </container>
    

    Since you'll control the content form the "child" component, you don't need to pass in the variables.

    public class ParentComponent {
    
    }
    

    Control the content of your layout component (parent) from within the child component.

    <t:ParentComponent xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter" t:content="text/html; charset=utf-8">
        body of c1 1, 2, 3
    </t:ParentComponent>