简体   繁体   中英

Liferay theme and portlet communication

I have a portlet embedded in a theme. The only solution that I found to permit the theme to get parameters values from the portlet is to use an intermediate database. What I did is that I created a table in the portlet and then I tried to access to this table from the theme:

Java Code in the portlet:

ExpandoTable table=null;
        try {
            table = ExpandoTableLocalServiceUtil.addTable(CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId(), User.class.getName(), "ClientTab");
        }
        catch (  DuplicateTableNameException dtne) {
            table=ExpandoTableLocalServiceUtil.getTable(CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId(), User.class.getName(), "ClientTab");
        }

The velocity code in the theme:

#set ($accountsTableName = "ClientTab")

#set ($accountsTable = $expandoTableLocalService.getTable($accountsTableName, $accountsTableName))

#if (!$accountsTable)
 <h2> The table ClientTab doesn't exist </h2>
#else
 <h2> Well The table ClientTab exists </h2>
#end

But the result that I got is:

The table ClientTab doesn't exist

I used those references to develop my code:

http://myjavaexp.blogspot.com/2013/01/liferay-expando-services.html

http://www.programcreek.com/java-api-examples/index.php?api=com.liferay.portlet.expando.DuplicateColumnNameException

http://www.liferay.com/fr/web/raymond.auge/blog/-/blogs/715049

If you just want to pass values from your portlet to the theme you can add new Velocity variables in your render method, like this:

HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(request);
Map<String, Object> velocityVariables = (Map<String, Object>)httpRequest.getAttribute(WebKeys.VM_VARIABLES);
if(velocityVariables == null) {
    velocityVariables = new HashMap<String, Object>();
}

velocityVariables.put("mySpecialKey", "mySpecialValue");
request.setAttribute(WebKeys.VM_VARIABLES, velocityVariables);

Then you can use the variable in your theme like this:

<h1>Here's the value added by my portlet --> $mySpecialKey</h1>

Don't forget the $ character on the front of the variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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