Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Seeya S Kudtarker
Using many classes for one portlet
April 3, 2013 4:18 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

We have one portlet class when we create a new portlet in a project.
I have my portlet package com.test.mis.portal in which I have my portlet class MISPortalActionUtil.java

Just to make it simple I want to have different classes like Employeedetails.java, AttendanceDetails.java .. and so on.

In portlet.xml file my portlet class name is given as:
<portlet-class>com.test.mis.portal.MISPortalActionUtil</portlet-class>

How should I access the other classes from the same package when i intend to use them.
Whenever I try to use functions from other classes like Employeedetails.java, I get an error which says that the particular function is not a part of com.test.mis.portal.MISPortalActionUtil

So how should I access functions from classes other than the portlet class which are in the same package?

Regards,
Seeya
Siby Mathew
RE: Using many classes for one portlet
April 3, 2013 5:49 AM
Answer

Siby Mathew

Rank: Expert

Posts: 268

Join Date: March 4, 2011

Recent Posts

HI Seeya,
In your portlet.xml, only your portlet class needs to be specified in <portlet-class>(eg: MyCustomPortlet).
In MyCustomPortlet, you can import and use any of your Util classes just like any other java class.

Thanks,
Siby
Seeya S Kudtarker
RE: Using many classes for one portlet
April 3, 2013 8:41 PM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Thanks Siby emoticon

I knew that but was wondering if there was a way to use more than one action class

Regards

Seeya
Rahul Pande
RE: Using many classes for one portlet
April 3, 2013 11:13 PM
Answer

Rahul Pande

Rank: Expert

Posts: 308

Join Date: July 6, 2010

Recent Posts

Hi Seeya,

This is possible if you use framework like Struts or Spring. Liferay also use Struts framework for this. Please look into portlet-custom.xml, liferay-portlet.xml, struts-config.xml, tiles-defs.xml in Liferay source for more information.

Are you using any of these framework ?

Thanks
Rahul
Seeya S Kudtarker
RE: Using many classes for one portlet
April 3, 2013 11:18 PM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Rahul Pande,

No.
An alternative idea which I thought of is create different portlets in the same project (I am using eclipse IDE)

But it is a headache to change the xml files. I have been doing it for past few hours without any success..
I am referring to Portlet to Portlet Communication wiki for that.

Can you help me with this?

Seeya
Rahul Pande
RE: Using many classes for one portlet
April 3, 2013 11:42 PM
Answer

Rahul Pande

Rank: Expert

Posts: 308

Join Date: July 6, 2010

Recent Posts

Hi Seeya,

If you want to separate code for each of your module like Employeedetails.java, AttendanceDetails.java .. and so on, then it is better to create different utility classes for these modules rather than creating separate portlets. You can put those classes in organized packages in same portlet. emoticon

HTH
Rahul
Seeya S Kudtarker
RE: Using many classes for one portlet
April 3, 2013 11:49 PM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Rahul,
thanks for the reply.

Your answer was my original question. emoticon

But I got the reply that one portlet can have one actionutil class and that I could create other classes and these new classes can be called by the actionutil class.

Can you please elaborate your solution you gave me just now? How can I call these separate util classes? since in portlet.xml, only one actionutil class can be specified..

Seeya
Rahul Pande
RE: Using many classes for one portlet
April 4, 2013 12:44 AM
Answer

Rahul Pande

Rank: Expert

Posts: 308

Join Date: July 6, 2010

Recent Posts

Hi Seeya,

You can have ONLY ONE portlet class per portlet and it is specified in portlet.xml using <portlet-class> tag. This class handles the life cycle of the portlet (init, render, processAction etc ..).

In your case I guess you must be extending your portlet class by GenericPortlet.

If you are processing something related to e.g Employee, then in process action you can call EmployeeDetail.java class and write your own code there. You can apply this for all your modules.

To identify which type of request (e.g Employee, Account etc...) needs to be processed, pass some parameter value from jsp and based on this parameter value call appropriate class in backend.


Thanks
Rahul
Seeya S Kudtarker
RE: Using many classes for one portlet
April 4, 2013 1:05 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Hi Rahul,
I am extending my portlet class by MVCPrtlet.
I will tell you where I am having problem.
I have my portlet class under com.test.mis.portal
In the portlet.xml file, I specify my portlet class in the <portlet-class> tag. Initially I had all my CRUD and other functions in this class. Just to make the code cleaner, I created separate classes(employee.java,attendance.java....) in the same package com.test.mis.portal where I am having my portlet class.
In my jsp I have to take input from user and I have something like
<portlet:actionURL name="addEmployeeDetails" var="addEmployeeDetails" />
<aui:form name="addEmployeeDetails" action="<%=addEmployeeDetails.toString() %>" method="post" >


As long as the addEmployeeDetails function was in portlet class it worked well. When I included it in employee.java ( com.test.mis.portal) it said that it cannot find the class in com.test.mis.portal.

You suggested to use a parameter-
To identify which type of request (e.g Employee, Account etc...) needs to be processed, pass some parameter value from jsp and based on this parameter value call appropriate class in backend.
How should I pass the parameter?

Seeya
Rahul Pande
RE: Using many classes for one portlet
April 4, 2013 2:30 AM
Answer

Rahul Pande

Rank: Expert

Posts: 308

Join Date: July 6, 2010

Recent Posts

Hi Seeya,

Thanks for explaining this in detail. emoticon

You are creating action URL using following tag

<portlet:actionURL name="addEmployeeDetails" var="addEmployeeDetails" />

When you submit the form with this action URL then, as per portlet life cycle process action method of the portlet class will be called. You can have multiple process action methods in your portlet class. For e.g

In Jsp

There are two action URLs

1<portlet:actionURL name="addEmployeeDetails" var="addEmployeeDetails" />

and
1<portlet:actionURL name="addAccountDetails" var="addAccountDetails" />


Portlet class
As you have two actions on the jsp, there must be two process action methods in your portlet class as follows
IMP: Name of the methods must be same as "name" attribute in <portlet:actionURL /> tag.

1
2public void addEmployeeDetails(ActionRequest actionRequest,
3            ActionResponse actionResponse) throws IOException, PortletException {
4       
5    }

and
1
2public void addAccountDetails(ActionRequest actionRequest,
3            ActionResponse actionResponse) throws IOException, PortletException {
4       
5    }


In this way you can have multiple process action methods in your portlet class.

In order to pass parameters to the method you can create url as follows
1
2<portlet:actionURL var="xyz">
3  <portlet:param name="param name" value="param value" />
4</portlet:actionURL>


Thanks
Rahul
Seeya S Kudtarker
RE: Using many classes for one portlet
April 4, 2013 2:39 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

<portlet:actionURL var="xyz">
<portlet:param name="param name" value="param value" />
</portlet:actionURL>

Above written is your code emoticon

So now when I want to access a method from a class which is other than the portlet class, param name which is in <portlet:param name="param name" value="param value" /> should be the name of the method of that class , right?

If you want to know what I exaclty want here it is:
I have a portlet class say portletutil.java in com.test package. I have 1.java and 2.java in the same package. And I want to access a method sat dosomething() from 1.java in jsp.

So using your above code it will be-
<portlet:actionURL var="xyz">
<portlet:param name="param name" value="param value" />
</portlet:actionURL>


I pefeclty understood your explanation of portlet classes. Problem comes when I access other classes..
I may sound dumb but do help me emoticon

Regards

Seeya
Rahul Pande
RE: Using many classes for one portlet
April 4, 2013 2:51 AM
Answer

Rahul Pande

Rank: Expert

Posts: 308

Join Date: July 6, 2010

Recent Posts

Hi Seeya,

So now when I want to access a method from a class which is other than the portlet class, param name which is in <portlet:param name="param name" value="param value" /> should be the name of the method of that class , right?


No, <portlet:param name="param name" value="param value" /> is used to pass string type data to process action method.

I have a portlet class say portletutil.java in com.test package. I have 1.java and 2.java in the same package. And I want to access a method sat dosomething() from 1.java in jsp.


In this case you can create a static method in e.g Abc.java and call that method from jsp using class name Abc.doSomething()

Thanks
Rahul
Seeya S Kudtarker
RE: Using many classes for one portlet
April 4, 2013 3:03 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Hey rahul,

Oh in that case I think I will create some classes abc.java , xyz.java and so on in the same package where portlet class is present and
call the methods of a.java, b.java in porltet class as follows:
abc newinst = new abc();
newinst.dosomething();

I knew the working of portlet class which you explained. My only problem was calling other classes in jsp

Regards

Seeya
Seeya S Kudtarker
RE: Using many classes for one portlet
April 4, 2013 3:13 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Rahul,

Just for the sake of curiosity, do you know how to pass the data and access methods from one portlet to another.
Brings me back to the previous question you answered emoticon
I have gone through some Wikis here but could not get it working.
So was just thinking if you have more information or summary you could provide.
I want to try to it.

Regards

Seeya
Rahul Pande
RE: Using many classes for one portlet
April 4, 2013 3:28 AM
Answer

Rahul Pande

Rank: Expert

Posts: 308

Join Date: July 6, 2010

Recent Posts

Hi Seeya,

Please refer this link.


Thanks
Rahul
Seeya S Kudtarker
RE: Using many classes for one portlet
April 4, 2013 3:31 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Hey Rahul,
I had gone through this your link earlier in the morning. emoticon
Will go through it in detail.

Thanks..
Regards
Seeya
Mohammad Azharuddin
RE: Using many classes for one portlet
April 4, 2013 3:55 AM
Answer

Mohammad Azharuddin

Rank: Liferay Master

Posts: 504

Join Date: September 17, 2012

Recent Posts

Hi

you want to create many portlet in single war...?that is using single liferay-display.xml,liferay-portlet.xml,portlet.xml,you create many portlet..?
Seeya S Kudtarker
RE: Using many classes for one portlet
April 4, 2013 4:00 AM
Answer

Seeya S Kudtarker

Rank: Regular Member

Posts: 187

Join Date: January 15, 2013

Recent Posts

Mohammad,

Is that possible? I might sound dumb but I really havent' tried it neither do I have any idea.
I wanted to create two portlets in one liferay project (I use eclipse IDE).
Can I use the same service.xml in this case? Or should I create separate service.xml files?
Before that,I have one more query..Can one liferay project have more than one service.xml files?

Seeya
Mohammad Azharuddin
RE: Using many classes for one portlet
April 4, 2013 4:16 AM
Answer

Mohammad Azharuddin

Rank: Liferay Master

Posts: 504

Join Date: September 17, 2012

Recent Posts

In liferay right click on your portlet and create new portlet..and eclipse will take care of everything. i will same same configuration files.same service.xml also