Wednesday, October 30, 2013

Body onload with PrimeFaces autoRun

To perform an action during page load, the normal practice is to invoke a javascript with the onload attribute in HTML body.

<html>
<head>
<script>
function load() {
alert("Page is loaded");
}
</script>
</head>
<body onload="load()">
<h1>Hello World!</h1>
</body>
</html>

With PrimeFaces <p:remoteCommand /> component, backingBean action could be called directly during page load.
by turning on the autoRun attribute to true only.
<p:remoteCommand name="testAutoRun"
    action="#{myBean.testAutoRun}"
    autoRun="true" />

The benefits with <p:remoteCommand /> are:
1. easy to use
2. No hassles to write any javascript
3. other pages are not affected (if facelets template is used)


Done!!

Sunday, October 27, 2013

How to get client id in Liferay PrimeFaces portlet

There is a convenient function in PrimeFaces to get client id of a component.
That is #{p:component('compId')}
The above function returns a full client id, eg. form:compId

But the full client id returned in Liferay portlet is not usable.
because Liferay portal is appending an unique id in front of form to make the client id unique.
eg. A8838:form:compId

Thus, the returned id is not a valid id in PrimeFaces or JSF context.

 JSTL functions could be used to solve this issue.

#{fn:substringAfter(p:component('compId')}

with the above syntax, the returned id will igore the text before the first ":"
Thus, returned id would be form:compId


Done!!

Thursday, October 24, 2013

Looping in JSF dataTable

To build a dataTable with dynamic columns in RichFaces,
we may use <c:foreach /> or <rich:columns /> in <rich:dataTable />
<rich:dataTable var="var" value="#{myBean.list}">
    <c:foreach var="column" value="#{myBean.columns}">
        <rich:column>
            <f:facet name="header">
                <h:outputLabel value="#{column.name}" />
            </f:facet>
            <h:outputLabel value="#{var[column.value]}" />
        </rich:column>
    </c:foreach>
</rich:dataTable>

But the same is not applicable in <p:dataTable />
because <ui:repeat /> and <c:foreach /> is not working in <p:dataTable />
Thus, the only solution to achieve dynamic column in <p:dataTable /> is to use <p:columns />
<p:dataTable var="var" value="#{myBean.list}">
    <p:columns var="colomn" value="#{myBean.columns}">
        <f:facet name="header">
            <h:outputLabel value="#{column.name}" />
        </f:facet>
        <h:outputLabel value="#{var[column.value]}" />
    </p:columns>
</p:dataTable>

With <c:foreach /> or <ui:repeat />, more flexibilities could be achieved.
Since they are not working in <p:dataTable />, the only way to achieve looping is <p:columns /> with less flexibilities.


Done!!

Monday, October 21, 2013

Accessing bean's property dynamically like a map

Assuming we have the following class
@ManagedBean(name="myBean")
public class MyBean {
    private String property1;
    private String property2;

    // getter and setting for property1, property2, ...
}

Normally we are accessing the properties in a bean from xhtml like syntax below.
#{myBean.property1}

But, recently found that we can actually accessing the properties of a bean like accessing a map.
#{myBean['property1']}

#{myBean['property2']}

in normally circumstances, it is nothing special from the traditional way.
But it is relatively useful if we want to dynamically access the properties in a bean.


Done!!

Friday, October 18, 2013

How to find deployed portlet id in Liferay

To find the targeted portlet id in Liferay, follow the below steps:

1. Login to Liferay portal as Administrator
    For user without Administrator rights, Portlet id could be retrieve from browser URL.

2. Navigate to Portlet Plugins
Go to > Control Panel > Portal > Plugins Configuration > Portlet Plugins


3. Click on the targeted portlet from the list, the portlet id will be shown in the details page.



Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...