Liferay Hook calling extension method - liferay

I have new action in a liferay hook which ideally will be calling methods created in an extension. But at run time when executing the action, it throws the exception
java.lang.ClassNotFoundException
for the methods created in the extension.
Has anybody created similar action in liferay hook? if so, what was the solution for this problem if encountered?
Here is my code:
public class ExampleStrutsAction extends BaseStrutsAction {
public String execute( HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = ParamUtil.get(request, "name", "World");
ThemeDisplay themeDisplay= (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
BSCDynamicDataListLocalServiceUtil.cloneDynamicDataListPageInSuborganization(the‌​meDisplay, name);
return "/portal/sample.jsp";
}
}

Try below code,
hook.xml
<hook>
<portal-properties>portal.properties</portal-properties>
<language-properties>
content/Language.properties
</language-properties>
<custom-jsp-dir>/META-INF/custom_jsps</custom-jsp-dir>
<struts-action>
<struts-action-path>/my_account/edit_user</struts-action-path>
<struts-action-impl>com.test.hook.action.EditUserAction</struts-action-impl>
</struts-action>
<struts-action>
<struts-action-path>/users_admin/edit_user</struts-action-path>
<struts-action-impl>com.test.hook.action.EditUserAction</struts-action-impl>
</struts-action>
</hook>
EditUserAction class
public class EditUserAction extends BaseStrutsPortletAction
{
#Override
public void processAction(final StrutsPortletAction originalStrutsPortletAction, final PortletConfig portletConfig,
final ActionRequest actionRequest, final ActionResponse actionResponse) throws Exception
{
// add your custom code
originalStrutsPortletAction.processAction(portletConfig, actionRequest, actionResponse);
}
#Override
public String render(final StrutsPortletAction originalStrutsPortletAction, final PortletConfig portletConfig, final RenderRequest renderRequest,
final RenderResponse renderResponse) throws Exception
{
//add your custom code
return originalStrutsPortletAction.render(portletConfig, renderRequest, renderResponse);
}
}
I have created above hook for EditUserAction in Control Panel
Hope it helps you!!

Related

How to get default page url in liferay?

I set default page URL in portal-ext.properties.
default.landing.page.path=/group/guest/home
Then create portlet with Controller :
public class UserPortrait extends MVCPortlet {
#Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse) {
}
#Override
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
super.processAction(actionRequest, actionResponse);
}
}
Now , how to get landing page URL in processAction method.
Simply, use com.liferay.portal.kernel.util.PropsUtil.get(String propertyName) to get any property from portal-ext.properties file.

How to create a new(original) struts path?

I have tried overriding existing StrutsPortletAction before with their existing struts path with success. However, I can't seem to do the same if I were to try creating my own struts action path.
<hook>
<custom-jsp-dir>/custom_jsps</custom-jsp-dir>
<struts-action>
<struts-action-path>/portal/set_viewers/</struts-action-path>
<struts-action-impl>com.mine.blogs.hook.BlogEntryViewerStrutsPortletAction</struts-action-impl>
</struts-action>
</hook>
The eclispe IDE gives me this error "/portal/set_viewers/" is not among possible values" and when I go ahead and deploy the built war anyways, tomcat errors as: com.liferay.portal.kernal.util.InstanceFactory can not access a member of class com.mine.blogs.hook.BlogEntryViewerStrutsPortletAction with modifiers ""
Tried with struts-action-path as /blogs/set_viewers/ failed as well.
This is the .java i'm using. Very basic actually.
package com.mine.blogs.hook;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import com.liferay.portal.kernel.struts.StrutsPortletAction;
public class BlogEntryViewerStrutsPortletAction implements StrutsPortletAction {
BlogEntryViewerStrutsPortletAction(){
super();
}
#Override
public void processAction(
PortletConfig arg0, ActionRequest arg1, ActionResponse arg2)
throws Exception {
// TODO Auto-generated method stub
System.out.println("process1");
}
#Override
public void processAction(
StrutsPortletAction arg0, PortletConfig arg1, ActionRequest arg2,
ActionResponse arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("process2");
}
#Override
public String render(
PortletConfig arg0, RenderRequest arg1, RenderResponse arg2)
throws Exception {
// TODO Auto-generated method stub
System.out.println("render1");
return null;
}
#Override
public String render(
StrutsPortletAction arg0, PortletConfig arg1, RenderRequest arg2,
RenderResponse arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("render2");
return null;
}
#Override
public void serveResource(
PortletConfig arg0, ResourceRequest arg1, ResourceResponse arg2)
throws Exception {
// TODO Auto-generated method stub
System.out.println("serve1");
}
#Override
public void serveResource(
StrutsPortletAction arg0, PortletConfig arg1, ResourceRequest arg2,
ResourceResponse arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("serve2");
}
}
And the corresponding liferay-hook.xml
<hook>
<custom-jsp-dir>/custom_jsps</custom-jsp-dir>
<struts-action>
<struts-action-path>/blogs_entry/set_viewers/</struts-action-path>
<struts-action-impl>com.mine.blogs.hook.BlogEntryViewerStrutsPortletAction</struts-action-impl>
</struts-action>
</hook>
Please try changing the struts action URL to something other than starting with "/portal". Liferay may be reserving "/portal" for portal level action paths.
For example,
<struts-action-path>/blogs_entry/set_viewers/</struts-action-path>
The error was because of the Constructor. I've removed it and it's now deploying properly

sendRedirect in custom action class

I am overriding one of the actions of the calendar portlet . I want to do some functionality then redirect to another JSP after that. I tried using the sendRedirect function as follows:
1.
String redirect = ParamUtil.getString(actionRequest , "redirect");
actionResponse.sendRedirect(redirect);
2.
actionResponse.sendRedirect ("/calendar/view") ;
3.
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
PortletURL url = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest), "8", themeDisplay.getPlid(),
PortletRequest.RENDER_PHASE);
url.setParameter("struts_action", "/calendar/view");
actionResponse.sendRedirect (url.toString()) ;
However, none of the methods cause the jsp to redirect to another one.
I am doing so in the processAction function and I am extending the BaseStrutsPortletAction.
Any suggestions how to solve it?
This is my overridden file :
public class TestAction extends BaseStrutsPortletAction {
public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig,
ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
String cmdConstants = ParamUtil.getString(actionRequest, Constants.CMD);
if (cmdConstants.equals(Constants.APPROVE)) {
//some function
actionResponse.setRenderParameter("test", "test");
originalStrutsPortletAction.processAction(portletConfig, actionRequest, actionResponse);
}
}
public String render(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, RenderRequest renderRequest,
RenderResponse renderResponse) throws Exception {
String cmdConstants = ParamUtil.getString(renderRequest, Constants.CMD);
if (cmdConstants.equals(Constants.APPROVE)) {
if (renderRequest.getParameter("test").equals("test")) {
return "/portlet/calendar/view.jsp";
}
}
return originalStrutsPortletAction.render(null, portletConfig, renderRequest, renderResponse);
}
}

Portlet Using Other Portlet

I am having two portlets with following action methods
A-Portlet
public void actionMethodA(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
System.out.println("Portlet A");
}
B-Portlet
public void actionMethodB(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
System.out.println("Portlet B");
}
Can we call actionMethodB in B-Portlet ?
Use PortletClassInvoker class to call a plugin method from another plugin.
Example:
try {
methodKey = new MethodKey(
ClassResolverUtil.resolveByPortletClassLoader(
"com.acme.SyncFactoryRegistry",
"custom-portlet"), "register", String.class,
Object.class);
}
catch (RuntimeException re) {
return;
}
PortletClassInvoker.invoke(
false, "1_WAR_customportlet",
_methodKey , arg1, arg2);

Get keyword of the search with struts Liferay

I want get the keywords of the search with the portlet of Search. I saw that in the url appear: "struts_action=/search/seach" then I looked for in strut-config.xml and find:
action path="/search/search" forward="portlet.search.search"
I am trying to do a strut but I don't know very much about struts and it doesn't work. This is the code of 'liferay-hook.xml':
<portal-properties>portal.properties</portal-properties>
<struts-action>
<struts-action-path>/search/search</struts-action-path>
<struts-action-impl>com.segmentationProject.searchAction.struts.SearchAction</struts-action-impl>
</struts-action>
portal.properties:
auth.public.paths=/search/search
SearchAction.java:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.struts.BaseStrutsAction;
public class SearchAction extends BaseStrutsPortletAction {
#Override
public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
System.out.println("inside the process ");
super.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
}
#Override
public String render(StrutsPortletAction originalStrutsPortletAction,PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {
// TODO Auto-generated method stub
System.out.println("inside the render");
return super.render(portletConfig, renderRequest, renderResponse);
}
}
Any idea about I missing or do wrong? I only want get the keywords and then do the search normally.
Thanks!
I think that this one will do the job :
String keywords = ParamUtil.getString(actionRequest, "keywords");
ParamUtil use the portlet namespace to retrieve parameters.
I think you must use originalStrutsPortletAction instead of super.
super.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
}
just replace super by originalStrutsPortletAction
originalStrutsPortletAction .processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
}

Resources