Saturday, October 25, 2014

Liferay Message Customization

Today we Customize liferay Error and Success Messages . Here If user provide hello as name and hello as password success message is shown otherwise error message is shown


Step 1:-Create Liferay Project 

In eclipse click  File-->New Liferay Plugin Project-->Give Project Name --> Finish

Step 2:-Create Liferay Portlet in the project 

Then Right Click on Project-->New  Liferay Portlet-->Give the portlet Class-->And select Generic Portlet-->next







Then check create resource bundle file check box


then give the Display Category and finish




Step 3:- Add the following code in view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<portlet:defineObjects />
<portlet:actionURL var="actionUrl" ></portlet:actionURL>

<liferay-ui:success key="success-key" message="Successfully" />
<liferay-ui:error key="error-key" message="error"/>

<form action="<%=actionUrl %>" method="post">
Name:<input type="text" name="name" ><br>
Password:<input type="password" name="pass"><br>
<input type="submit" value="Submit">
</form>


Explanation of view.jsp

Here we use
<liferay-ui:success key="success-key" message="Successfully" />
<liferay-ui:error key="error-key" message="Error"/>

the success-key and error-key both are set in Java Class 
the message Successfully is shown if success-key is found(in green color) and
the message Error is shown if error-key is found(in red color)

Step 4:- Add the following code in your Class

 @Override
 public void processAction(ActionRequest request, ActionResponse response)                                throws   PortletException,  IOException {
String name = ParamUtil.getString(request, "name");
String password = ParamUtil.getString(request, "pass");
if(name.equalsIgnoreCase("hello")&& password.equalsIgnoreCase("hello"))
{
SessionMessages.add(request,"success-key");
}
else{
SessionErrors.add(request, "error-key");
}
}

Explanation of Class

1) SessionMessages.add(request,"success-key");

This "success-key " is added if user enter hello as name and hello as Password and this must match in view.jsp 

<liferay-ui:success key="success-key" message="Successfully" />

2) SessionErrors.add(request, "error-key");

This "error-key" is added if user not added hello as name and hello as password this key must match in view.jsp as

<liferay-ui:error key="error-key" message="Error"/>


Step 5:- Deploy Your Portlet on server

Right click on build.xml --> Run on Ant build

Step 6:- Open Browser and hit http://localhost:8080/ 

Add your portlet on page and put hello hello



If provide any other value



Step 6:- Disable Liferay Default error message(Your Request Failed to complete)

 Add these two lines in your Class

PortletConfig portletConfig =         (PortletConfig)request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);

SessionMessages.add(request, ((LiferayPortletConfig)portletConfig).getPortletId() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

and again deploy



As you see only our message is shown 

 If you want that your Message is not hard Coded and come from properties file



Step:-7 Provide entries of Language.properties in portlet.xml

Their is a Language.properties file is created in content folder (Due to Step 2) provide the entry of this in portlet.xml(actually this is already done by eclipse)

<resource-bundle>content.Language</resource-bundle>

Step:-8 Provide entries in Language.properties 

Put entries in Language.properties file

Successfully=This Success Message come from Language.properties file
Error=This Error Message come from Language.properties file

These "Succeessfully" and "Error" must match with message of view.jsp as

<liferay-ui:success key="success-key" message="Successfully" />
<liferay-ui:error key="error-key" message="Error"/>

Step:-9 Deploy Your Portlet







So first Liferay try to find the message in Language.properties if following key is not found then show the normal message described in view.jsp ie if you delete

Successfully=This Success Message come from Language.properties file

from properties file then error message come from Language.properties file but success message is from jsp file

Success message from jsp


Error message from Language.properties file                                                                                           








HOPE THIS WILL HELP



2 comments:

Total Pageviews

Number Of Unique Visitor

Free counters!