Liferay MVC portlet - first steps
Liferay MVC portlet - first steps
When you generate an MVC Portlet in Liferay Workspace - you’ll get the following code (sample):
Portlet class:
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + UserSearchPortletKeys.UserSearch,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
public class UserSearchPortlet extends MVCPortlet {
}
Portlet keys class:
public class UserSearchPortletKeys {
public static final String UserSearch = "usersearch";
}
Here is a list of first steps to modify here.
1) Change generated portlet name.
Change generated name with Liferay-style portlet ID. For this sample:
UserSearchPortletKeys:
public static final String PORTLET_ID = "com_liferdev_user_search_portlet_UserSearchPortlet";
UserSearchPortlet:
"javax.portlet.name=" + UserSearchPortletKeys.PORTLET_ID,
Use full class name with ‘_’ instead of ‘.’
2) Add display name.
Add constant and use it in portlet:
UserSearchPortletKeys:
public static final String DISPLAY_NAME = "User Search Portlet";
UserSearchPortlet:
"javax.portlet.display-name=" + UserSearchPortletKeys.DISPLAY_NAME,
This name will be displayed for portlet on Liferay page.
3) Change display category:
Use required category name to display portlet under that category:
UserSearchPortletKeys:
public static final String DISPLAY_CATEGORY = "LifeDev";
UserSearchPortlet:
"com.liferay.portlet.display-category=" + UserSearchPortletKeys.DISPLAY_CATEGORY,
Otherwise, portlet will be displayed under default “Sample” category.
4) Add css-class-wrapper:
UserSearchPortletKeys:
public static final String CSS_CLASS_WRAPPER = "lifedev-user-search-portlet-wrapper";
UserSearchPortlet:
"com.liferay.portlet.css-class-wrapper=" + UserSearchPortletKeys.CSS_CLASS_WRAPPER
This is needed to reference portlet in theme for styling.
5) Remove generated resources/content/Language.properties file
It’s a best practice to store Language properties in a single resource bundle module.
6) Create required MVC Commands (render/action/resource) and use them in portlet.
It’s a best practice to not modify portlet’s class code, but use a separate command for each required action.
Here is sample code for this example:
Portlet:
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=" + UserSearchPortletKeys.DISPLAY_CATEGORY,
"com.liferay.portlet.instanceable=true",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + UserSearchPortletKeys.PORTLET_ID,
"javax.portlet.display-name=" + UserSearchPortletKeys.DISPLAY_NAME,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user",
"com.liferay.portlet.css-class-wrapper=" + UserSearchPortletKeys.CSS_CLASS_WRAPPER
},
service = Portlet.class
)
public class UserSearchPortlet extends MVCPortlet {
}
Portlet keys:
public class UserSearchPortletKeys {
public static final String PORTLET_ID = "com_liferdev_user_search_portlet_UserSearchPortlet";
public static final String DISPLAY_NAME = "User Search Portlet";
public static final String DISPLAY_CATEGORY = "LifeDev";
public static final String CSS_CLASS_WRAPPER = "lifedev-user-search-portlet-wrapper";
}
Hope, this will help you for developing MVC portlets 😏
Comments
Post a Comment