Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using Model Hints in Liferay

    • 0
    • 2
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 708
    Comment on it

    Liferay Service Builder provides users a way to customize input fields and model entities by specifying related information as model hints in a single file called portlet-model-hints.xml in projects docroot/WEB-INF/src/META-INF folder. But we need to run Service Builder everytime we update our portlet-model-hints.xml file and redeploy the portlet to see changes.

    These are called as model hints as they define how entities should be presented to end users and can also specify the size of database columns used to store the entities.

    When we display a portlet, it first checks the model hints we specified and customizes the form's input fields based on these hints.

    Example: I have created below entity in my service.xml

    <entity name="AdditionalEmailAddress" local-service="true"
            remote-service="true" cache-enabled="true">
            <!-- PK fields -->
            <column name="additionalEmailAddressId" type="long" primary="true"
                id-type="identity" />
            <!-- Audit fields -->
            <column name="additionalEmail" type="String" />
            <column name="additionalEmailtoken" type="String" />
            <column name="emailVerified" type="boolean" />
            <column name="userId" type="long" />
            <column name="createdDate" type="Date" />
            <column name="modifiedDate" type="Date" />
            <!-- Order -->
            <order by="asc">
                <order-column name="additionalEmailAddressId" />
            </order>
            <!-- Finder methods -->
            <finder name="UserId" return-type="Collection">
                <finder-column name="userId" />
            </finder>
        </entity>
    

    Now look at the model hints file that Service Builder generated for the AdditionalEmailAddress portlet.

    <?xml version="1.0"?>
    
    <model-hints>
        <hint-collection name="URL">
            <hint name="max-length">500</hint>
        </hint-collection>
        <model name="com.idsureity.service.model.AdditionalEmailAddress">
            <field name="additionalEmailAddressId" type="long" />
            <field name="additionalEmail" type="String" />
            <field name="additionalEmailtoken" type="String">
                <hint-collection name="URL" />
            </field>
            <field name="emailVerified" type="boolean" />
            <field name="userId" type="long" />
            <field name="createdDate" type="Date" />
            <field name="modifiedDate" type="Date" />
        </model>
    </model-hints>
    

    To add hints to a field, add a hint tag inside its field tag. For example, I've added a display-width hint to specify the pixel width that will be used when displaying the field. The default pixel width is 350. In below example we are using the display-width hint in a field element with 50 pixels:

    <field name="additionalEmail" type="String">
        <hint name="display-width">50</hint>
    </field>
    

    To see effect of hint on that field run the Service Builder again and redeploy the portlet project. It will display the width of that field in the AlloyUI input form.

    We can also use the default-hints element to define a list of hints that will be applied to every field of a model. For example, adding the following element inside a model element applies a display-width of 100 to each field of that model:

    <default-hints>
        <hint name="display-width">100</hint>
    </default-hints>
    

    We can give maximum size to database column by using the max-length hint. The default max-length value is 75 characters. Example:

    <field name="additionalEmail" type="String">
        <hint name="display-width">50</hint>
        <hint name="max-length">200</hint>
    </field>
    

    Run Service Builder and redeploy your portlet project af

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: