Custom Action(Events) Hook in Liferay


Today we will discuss about Custom Action Hook  in Liferay. In liferay there are some portal events you can see those events in portal.properties.These events are:-

  • application.startup.events
  • application.shutdown.events
  • login.events.pre
  • login.events.post
  • logout.events.pre
  • logout.events.post
  • servlet.service.events.pre
  • servlet.service.events.post
  • servlet.session.create.events
  • servlet.session.destroy.events

The action of these events are defined in portal.properties so we need to extend portal.properties to create a custom action.


So lets start step by step:-





Step 1:-Create Liferay hook project
File-->New-->Liferay Plugin Project-->Provide name-->Select hook in plugin type-->Finish.
You can see snapshot in step 2 of Portal Properties Hook in Liferay

Step 2:-Create  hook inside project
Right click on project-->New-->Liferay hook Configuration-->tick portal.properties-->Next.



then this pop up is open:-




then click on add another pop up is open where you can select the event click on select event button.This time pop up open with events as:-



Select login.events.pre and click ok.Now Click on New button of previous figure to add our class as:-



Provide Classname, java package and select  superclass  as Action and click ok.



Now Ok and finish.This will create portal.properties file automatically also provide entry in liferay-hook.xml.

Step 3:-Check Generated Code
By using eclipse ide portal.properties file automatically created as:-

portal.properties

and liferay-hook.xml as:-

liferay-hook.xml

Step 4:-Provide your custom code
Open MyCustomLoginAction.java from package com.test and paste this code:-

MyCustomLoginAction.java

Step 5:-Check Output
Now deploy your hook and login to liferay as soon as you login the eclipse console shows:-

My Login Pre event


Project Structure




You can Download Source code from Custom Action Hook in Liferay





Hope this will Help.....

Adding Struts Action by Hook in Liferay


Today we will discuss about Adding Struts action in Liferay by using Hook.Before reading this blog you must read my previous blog on  Override Struts Action by Hook in Liferay. Adding Struts action is similar to overriding Struts Action but the difference is :-

For Overriding we extends BaseStrutsPortletAction Class.
For Adding we extends BaseStrutsAction Class.

So lets start step by step:-

Step 1:-Create Liferay hook project
File-->New-->Liferay Plugin Project-->Provide name-->Select hook in plugin type-->Finish.
You can see snapshot in my previous article (Step 1)   Portal Properties Hook in Liferay.





Step 2:-Create Action Class
Create a package com.aditya.demo inside src and than create a class as:-

AddingStrutsAction.java

Explanation:-
In this class we extend BaseStrutsAction class and just provide path of jsp.

Step 3:-Create entry in liferay-hook.xml
Provide the entry of your action class in liferay-hook.xml:-

 liferay-hook.xml

Explanation:-
In this xml we provide the path ie URL and implementing class. For hitting this URL we have to append c as:
http://localhost:8080/c/portal/liferayiseasy 

Step 4:-Create required jsp
Create a jsp hook for the jsp as we return our path from action class as "/portal/message.jsp".

So create a structure in docroot as:-
docroot\custom_jsps\html\portal\message.jsp

Our message.jsp contain just one line:-

message.jsp

and provide entry in liferay-hook.xml as:-
 <custom-jsp-dir>/custom_jsps</custom-jsp-dir>

when you deploy your hook this jsp is available in
"\tomcat-7.0.42\webapps\ROOT\html\portal".

That's it when you hit "http://localhost:8080/c/portal/liferayiseasy" liferay show login page provide credential and you can see 
message .jsp.When you hit the URL and you don't want to provide the credential just make the URL public by property hook.

Step 5:-Make URL to public
Just create Portal Property Hook and override auth.public.paths as:-
auth.public.paths=/portal/liferayiseasy

Now finally your liferay-hook.xml become :-

liferay-hook.xml

For detail of how to create Portal Properties hook you may follow my previous blog Portal Property Hook in Liferay


That's it hit "http://localhost:8080/c/portal/liferayiseasy" and you see output as:-
Welcome here Aditya Bhardwaj

Project Structure





You can Download Source code from Adding Struts Action Hook in Liferay





Hope this will Help....

Override Struts Action by Hook in Liferay


Today we will discuss about Struts Action hook.By using Struts Action hook we can achieve two things:-
1)Overriding existing struts Action
2)Add new Struts Action

Today we will discuss about Overriding existing struts Action. Every time you login a Struts Action is called.Struts use a file called struts-config.xml you can find this file inside WEB-INF .

For overriding Struts Action your custom class must extend BaseStrutsPortletAction which implements  StrutsPortletAction which  contain processAction().
So Lets start step by step:-





Step 1:-Create Liferay hook project
File-->New-->Liferay Plugin Project-->Provide name-->Select hook in plugin type-->Finish.
You can see snapshot in my previous article (Step 1)   Portal Properties Hook in Liferay.

Step 2:-Create Action Path
Open Struts-config.xml (\webapps\ROOT\WEB-INF) and search for login action you will find entries as:-

struts-config.xml

As you can see path "/login/login" goes to "LoginAction" Class. We change this class to our Custom class.So just provide those entries in liferay-hook.xml as:-


liferay-hook.xml

Step 3:-Create Action Class
Inside "docroot/WEB-INF/src" create package com.test and create a class with name MyCustomAction as:

MyCustomAction.java

Explanation:-
Here we create our processAction() and inside this method we call the original processAction() so that flow of liferay not break.In place of SOP you can write your own logic.Similarly for render().

Step 4:-Check Output
Deploy your hook and hit localhost:8080 and then press SignIn button on the top right.Your eclipse console shows:-

My Custom Render Method is Called

Then fill the values in login form and press Sign In



Your eclipse console shows:-

My Custom Process Action Method is Called

Project Structure




You can Download Source code from Override Struts Action Hook in Liferay





Hope this will Help....

Language Properties Hook in Liferay


Today we will discuss about Language Properties hook.Language property hook are used to overriding the existed portal language properties like Language_en.properties file for overriding of english language file.We can also add new properties by using Language Property hook.

Today we change :-
1) Button text Sign In  to Login .
2)Error message This Field is required to Please enter this field.




So Lets start this step by step:-

Step 1:-Create Liferay hook project
File-->New-->Liferay Plugin Project-->Provide name-->Select hook in plugin type-->Finish.
You can see snapshot in my previous article (Step 1)   Portal Properties Hook in Liferay.





Step 2:-Create  hook inside project
Right click on project-->New-->Liferay hook Configuration-->tick Language.properties-->Next



then click on Add and provide name of property file which you want to override in my case i provide Language_en.properties



then click Ok and Finish.

Step 3:-Override Properties
Open Language_en.properties file which is inside content folder.Now if you attached source code in your eclipse press (ctrl+shift+r) and search for Language_en.properties



and open it .Now copy all the content of Language_en.properties of liferay file to our Language_en.properties file.

Now search for Key:-
1)this-field-is-required and change the value to Please enter this field.Ex- this-field-is-required=Please enter this field.
2)sign-in and change to Login.Ex-  sign-in=Login


Step 4:-Entry in liferay-hook.xml
This entry is automatically created by eclipse as:-
<hook>
<language-properties>
content/Language_en.properties
</language-properties>
</hook>


Step 5:-Check output
Deploy your hook and put out of box language portlet on your page and select english ie American Flag



And thats it refresh your page to see the output as:-



Here you can see Sign In change to Login and This field is required change to Please enter this field.

Project Structure



You can Download Source code from Language Property Hook in Liferay





Hope this will Help....

Portal Properties Hook in Liferay

Today we will discuss about Portal Properties hook.When we Create Account in liferay we have to provide the value of captcha today we disable the captcha field using Portal Properties hook.

We have portal.properties file inside portal-impl (\ROOT\WEB-INF\lib) which contain some properties.Here some properties have multiple values and some have only one value like true/false.

All properties of portal.properties can not be override,we can override only those properties that is mentioned in dtd file you can download dtd file from liferay-hook_6_2_0.dtd.




So Lets start this step by step:-

Step 1:-DownLoad dtd file
First download liferay-hook_6_2_0.dtd file and check property related to captcha .Verify that any property is related to our requirement is available or not. Ex- we found property
captcha.check.portal.create_account






Step 2:-Create Liferay hook project
File-->New-->Liferay Plugin Project-->Provide name-->Select hook in plugin type-->Finish





Step 3:-Create  hook inside project
Right click on project-->New-->Liferay hook Configuration-->tick portal.properties-->Next



Then in specify properties to override click on Add button




Then Add Property override box is open click on select and select 
captcha.check.portal.create_account and provide value to false



Click ok and finish.

Step 4:-Check generated files
All files are generated automatically you can see :-

a)portal.propertise(inside src)

captcha.check.portal.create_account=false

b)liferay-hook.xml(inside web-inf)
<hook>
<portal-properties>portal.properties</portal-properties>
</hook>

Step 5:-Check output
Deploy the hook by right click on build.xml-->Run As-->Ant Build. Go to your browser click on sign in and than Create Account link:-




As you can see there is no Captcha here.

Project Structure




Open/Close Dialog Box using AUI in Liferay


Today we will discuss how to Open and Close a Aui Dialog box in liferay.In this we create a button on click of button a form is opened in dialog box which contain two input fields name , password and two buttons one for Submit the form and second for cancel.On Submit form is Submitted and details are shown in the dialog box .On Cancel form is simply closed so lets start this step by step.


Step 1:-Create Liferay project and Change view.jsp
Create a liferay project and then create a Generic portlet in it.Open view.jsp and paste the content:-

view.jsp
Explanation

Here we create a renderUrl with param value loginPage. On click of button with id="login" Liferay.Util.openWindow function is called which send the render Request to our controller with a parameter loginPage.





Step 2:-Handle Render Request inside Controller
Just fetch the path attribute and if it equals to loginPage then include login.jsp ie

String path = ParamUtil.getString(renderRequest, "path");
if (path.equalsIgnoreCase("loginPage")) 
{
include("/html/demo/login.jsp", renderRequest, renderResponse);
}

Step 3:-Create login.jsp
Inside login.jsp we just create a simple form with two text fields and two buttons one for cancel and one for submit and a actionUrl for submittion of form:-

login.jsp

Step 4:-Handle Action Request inside Controller
When Submit button is clicked processAction method of our Controller is called

Demo.java
Explanation:-
Here we fetch the attributes that comes in Action request and set in response so that we can use in detail page.Here we also set a third attribute :-

response.setRenderParameter("path", "details");
This attribute help to determine the path in our doView() .


Step 5:-Send Controller to Details Page
The attribute set in as  "path" inside processAction() is used in doView() because after processAction() doView() is called as:-

Demo.java
Explanation:-
Here we fetch the attributes that are set as Render Parameter in processAction and again set in render Request so that they are available in details.jsp.

Step 6:-Create details.jsp
The details.jsp is simple and self explanatory:
details.jsp

Thats it this will open a dialog box and show details inside this.But what about CLOSE BUTTON.For Closing a Dialog box these steps are necessary:-

Step 7:-Modify login.jsp and view.jsp
Paste this code inside login.jsp .Here this method internally call method closeYourPopUp which is inside view.jsp
login.jsp
view.jsp


Thats it Complete code of view.jsp,login.jsp and Controller is:-

view.jsp
login.jsp
Demo.java

Output





Project Structure



That's it.

You can download source code from Aui Dialog Box in Liferay





Hope this will help......

 Related Post:-

Spring MVC portlet in Liferay

Form Handling in Spring Portlet

Many To Many Relationship mapping in Liferay Services

Portlet Name space Parameter in liferay

Form Submittion using Ajax(AUI) in Liferay

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven







Form Submission Using AJAX(AUI) in Liferay

Form Submission Using AJAX(AUI)


Today we will discuss how to Submit a form using Ajax .For sending Ajax request we are using Alloy UI (aui) tags.Then finally Print the form data to Success page.The important thing is when we use Ajax the response is come back in Json(or Xml) form then how we redirect to Success page?
For achieving this we send another request from Ajax success part by using Java Script.So Lets start step by step:-

Step 1:-Create Liferay project and Change view.jsp
Create a liferay project and then create a Generic portlet in it.Open view.jsp and paste the content

view.jsp

Explanation:-
1) In line (32-36) we create a simple form in which on click of submit button submitForm() is called.
2)Inside submitForm() we send a Ajax call in which we are using id of form for sending data (Name,Password)of form to Controller.
3)On success  we parse the Json Data send by controller and then send another render Request by using java script (window.location.href) and appending the data in URL so that we can display on Success page.






Step 2:-Create Your controller
Inside your controller paste this content:-

Demo.java

Explanation:-
1)Ajax Call comes to serveResource() here we fetch the form data and put in Json object and send back the response.
2)On Success part of Ajax (line 23 of view.jsp) we send another render Request by using window.location.href and append parameters.This call comes to doView().
3)Inside doView() we check the parameter(path) that is passed in render Request:-

<portlet:renderURL var="succcessURL">
<portlet:param name="path" value="success"/>
</portlet:renderURL>

if path is success the we get the parameters and set in attributes so that they are available in success.jsp.After that we include success.jsp.(line 28-33) .

Step 3:-Create success.jsp
Just paste this content to success.jsp:-

success.jsp
Step 4:-Change Required name space
We are using simple form so we have to change required namespace parameter to false inside liferay-portlet.xml.For detail explanation on this you may refer my previous blog Portlet Name Space in liferay.

<requires-namespaced-parameters>
    false
</requires-namespaced-parameters>

That's it.

You can download source code from Form Submittion By Ajax






Hope this will help....

 Related Post:-

Spring MVC portlet in Liferay

Form Handling in Spring Portlet

Many To Many Relationship mapping in Liferay Services

Portlet Name space Parameter in liferay

Open/Close AUI Dialog Box in Liferay

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven








Role Checking in Liferay for Show/Hide Content

Show/Hide Content on basis of Role in Liferay


Today we will discuss how to Show/Hide content on the basis of role or we can say that how we perform Role checking in liferay. For achieving this we create two roles teacher and student then we create two users Student and Teacher and assign role accordingly.Then finally Check roles and show/hide content. Lets start step by step:-

Step 1:-Create Two Roles
Go to control panel-->Users-->Roles-->Add-->Regular Roles
Then provide Name ,title and description as:-



and press save .Similarly create Student Role.





Step 2:-Create Two Users
Then create two users and assign roles to it. 
Go to control panel-->Users and Organizations-->All users-->Add User-->And provide details as:-



then click save.After that provide a password and click save .Similarly create another user with name Student.

Step 3:-Assign Role to Users
Go to control panel-->Roles-->On Teacher Click action-->Assign Users-->Available-->Selected Users Teacher-->Update Association.
Similarly assign Student role to student user.

Step 4:-Check Role in jsp

view.jsp
Explanation:-

List<Role> userRoles = themeDisplay.getUser().getRoles();
this will return List because one User can have many Roles so we iterare the List and and Checking Role name and on the basis of Role print statement accordingly.


Step 5(Optional):-Check Role in Controller
If you want to check role in your controller then:-

Controller.java

Step 6:-Check Output
Logout from Admin(test@liferay.com) and then

1)Login via teacher@gmail.com and add your portlet on page you can see output as:-




2)Login via student@gmail.com and add your portlet on page you can see output as:-



As you can see output is Changed on the basis of Users.

You can download source code from Role Checking in Liferay






Hope this will help....

 Related Post:-

Spring MVC portlet in Liferay

Form Handling in Spring Portlet

Many To Many Relationship mapping in Liferay Services

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven