Foros de discusión

How can I add the custom MVC ResourceCommand in Out-of-Box portlet

Vishal Shah, modificado hace 7 años.

How can I add the custom MVC ResourceCommand in Out-of-Box portlet

Junior Member Mensajes: 33 Fecha de incorporación: 1/02/12 Mensajes recientes
Hi,

In Liferay DXP, How can I add the custom MVCResourceCommand in Out-of-Box portlet (Ex. Search Portlet)?
Vishal Shah, modificado hace 7 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

Junior Member Mensajes: 33 Fecha de incorporación: 1/02/12 Mensajes recientes
Hi David H Nebinger,

As per given link, it's overriding the MVCResourceCommand which already exist in the system. But my original question is that Portlet does not exist the any of ResourceCommand classes that time I have to add my own class and do my functionality.

For Example, there is out of box SearchPortlet, it doest not exists any XXXResourceCommand classes. AJAX control code is written in serveResource method of the Search MVC Portlet (SearchPortlet - MVCPortlet). So I have to add my extra code when my custom ajax call comes.

I think two alternative way for this customizing:
1. I can add new custom XXXResourceCommand classes for handling my custom AJAX control.
2. I can modify the serveResource method MVCPortlet. - (No idea how can I modify this code in DXP)

So how can I do this? I try first way but not configure properly may be.

This below is my code which I follow 1st way.

view.jsp

I add the code fragment in jsp file

<portlet:resourceurl var="searchResourceURL" id="searchResource">
		<liferay-portlet:param name="cmd" value="seachContent" />
</portlet:resourceurl>
<aui:script>
AUI().use('aui-base','aui-io-request',function (A) {

var myAjaxRequest=A.io.request('&lt;%=searchResourceURL%&gt;',{
					dataType: 'json',
					sync:true,
					method:'POST',
					data:{
						<portlet:namespace />searchKey:inputValue,
					},
					autoLoad:false,
					on: {
					success:function(){
						var data=this.get('responseData');
						console.log(data);
                                                alert(data);
					}}
				});
				myAjaxRequest.start();

});
</aui:script>



SearchResourceCommand.java

package com.vks.liferay.search; // this my custom package

// Assume all imports available here

@Component(immediate = true, property = { "javax.portlet.name=com_liferay_portal_search_web_portlet_SearchPortlet",
		"mvc.command.name=searchResource", "service.ranking:Integer=100" }, service = MVCResourceCommand.class)
public class SearchResourceCommand implements MVCResourceCommand {

	@Override
	public boolean serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
			throws PortletException {
		System.out.println("&gt;&gt;&gt;before ");

		String resourceID = GetterUtil.getString(resourceRequest.getResourceID());
		String cmd = ParamUtil.getString(resourceRequest, "autoComplete");
		String searchKey = ParamUtil.getString(resourceRequest, "searchKey");
		System.out.println("&gt;&gt;&gt;after ");
		return mvcResourceCommand.serveResource(resourceRequest, resourceResponse);
	}

	protected MVCResourceCommand mvcResourceCommand;

	@Reference(target = "(&amp;(osgi.web.symbolicname=com.liferay.portal.search.web) (javax.portlet.name=com_liferay_portal_search_web_portlet_SearchPortlet))")
	public void setMvcResourceCommand(MVCResourceCommand mvcResourceCommand) {
		this.mvcResourceCommand = mvcResourceCommand;
	}

	public MVCResourceCommand getMvcResourceCommand() {
		return this.mvcResourceCommand;
	}
	
	@Reference(target = "(osgi.web.symbolicname=com.liferay.portal.search.web)")
    protected ServletContext servletContext;

}



bnd.bnd
Bundle-Name: search-jsp-hook
Bundle-SymbolicName: com.vks.liferay.search
Bundle-Version: 1.0.0
Fragment-Host: com.liferay.portal.search.web;bundle-version="1.1.2"
-sources: true
Web-ContextPath:/search-jsp-hook


This whole code I post here. I try for this but it's not binding with SearchPortlet.

Is it wrong anything in SearchResourceCommand.java file because it's not binding with the SearchPortlet.
thumbnail
David H Nebinger, modificado hace 7 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
You can do a JSP fragment bundle to replace the JSP page w/ your own. In there you're going to add whatever to invoke your serve resource method.

For your resource command implementation, you just need to assign it to the search portlet's name. The Liferay MVC framework will wire it all together and the search portlet will act like it has always had the resource command.
Ghenadii Batalski, modificado hace 6 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

New Member Mensajes: 7 Fecha de incorporación: 31/07/17 Mensajes recientes
Is the issue solved yet? I've the same problem: my own MVCResourceCommand implementation is not in a classpath even the fragment bundle deployed successfully and calls also the modified resource url.

Regards, Gena
Ghenadii Batalski, modificado hace 6 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

New Member Mensajes: 7 Fecha de incorporación: 31/07/17 Mensajes recientes
update: it is on a class path but won't be picked out, even the ajax request contains the right parameter. Requests just routed to SearchPortlet despite my added resource command.
thumbnail
David H Nebinger, modificado hace 6 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
It does work, Gena, I'm just not sure what you are (or are not) doing...

If you create a bundle with a unique ResourceMVCCommand that has it's own mvc.command.name but is wired up to an existing MVCPortlet implementation, this is not handled any differently from OSGi than if the class was contained in the original bundle.

That said, if I create an MVCPortlet extension and override how the command handlers are found, I could prevent additional commands from being added.

I think one issue the OP had was combining the ResourceMVCCommand class into the JSP fragment bundle; this is a no-no because fragment bundles are not executable, they don't become active, they just replace files from the original bundle. Therefore they cannot really add new components directly. Any new or overriding ResourceMVCCommand classes would need to be separated out to their own bundle.
Ghenadii Batalski, modificado hace 6 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

New Member Mensajes: 7 Fecha de incorporación: 31/07/17 Mensajes recientes
Hi David, it works!

If you create a bundle with a unique ResourceMVCCommand that has it's own mvc.command.name but is wired up to an existing MVCPortlet implementation, this is not handled any differently from OSGi than if the class was contained in the original bundle.

This was also my expectation...


I think one issue the OP had was combining the ResourceMVCCommand class into the JSP fragment bundle; this is a no-no because fragment bundles are not executable, they don't become active, they just replace files from the original bundle. Therefore they cannot really add new components directly. Any new or overriding ResourceMVCCommand classes would need to be separated out to their own bundle.


I've splitted the fragment into two bundles, just like you suggested. The bundle with the new MVCResourceCommand also may not be a fragment
(i removed a fragment host entry) to work.

Many thanks, even it took me about two days of work...

Regards Gena
Francesco Cincotti, modificado hace 5 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

New Member Mensajes: 5 Fecha de incorporación: 1/10/10 Mensajes recientes

Hi Gena,

I'm facing the same issue on the Calendar portlet: I tried to follow the LR guidelines and this thread, but neither can I add a new resource (I get calendar-does-not-serve-unknown-resource-x) nor can I override the 'calendarResources' resource id. Would you pls share your implementation ?

Thank you very much

Francesco

Ghenadii Batalski, modificado hace 5 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

New Member Mensajes: 7 Fecha de incorporación: 31/07/17 Mensajes recientes

Hello Francesco,

 

in my case i wanted to add custom ResourceCommand to an existing portlet. The extention to the existing portlet were done via the fragment. I've modified a JSP fragment and inserted the call to 

<liferay-portlet:resourceURL id="searchContent" var="searchResourceURL" copyCurrentRenderParameters="false"/>

The resource searchContent  is served via newly created MVCResourceCommand named searchContent:

@Component(immediate = true,
        property = {"javax.portlet.name=" + SearchPortletKeys.SEARCH,
                "mvc.command.name=searchContent",
                "service.ranking:Integer=100000"},
        service = MVCResourceCommand.class)
public class AutoCompleteSearchCommand implements MVCResourceCommand {

but i should create a completeley new bundle for AutoCompleteSearchCommand to be recognized by osgi.

It couldn't be done just by adding the AutoCompleteSearchCommand to the fragment because fragment accepts only modifications on existing artefacts within the Fragment-Host and the new command didn't exist. I hope, the information helps.

 

Kind regards, Gena

Testing Test, modificado hace 5 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

New Member Mensajes: 5 Fecha de incorporación: 26/03/12 Mensajes recientes
Ghenadii Batalski:

Hello Francesco,

 

in my case i wanted to add custom ResourceCommand to an existing portlet. The extention to the existing portlet were done via the fragment. I've modified a JSP fragment and inserted the call to 

<liferay-portlet:resourceURL id="searchContent" var="searchResourceURL" copyCurrentRenderParameters="false"/>

The resource searchContent  is served via newly created MVCResourceCommand named searchContent:

@Component(immediate = true,
        property = {"javax.portlet.name=" + SearchPortletKeys.SEARCH,
                "mvc.command.name=searchContent",
                "service.ranking:Integer=100000"},
        service = MVCResourceCommand.class)
public class AutoCompleteSearchCommand implements MVCResourceCommand {

but i should create a completeley new bundle for AutoCompleteSearchCommand to be recognized by osgi.

It couldn't be done just by adding the AutoCompleteSearchCommand to the fragment because fragment accepts only modifications on existing artefacts within the Fragment-Host and the new command didn't exist. I hope, the information helps.

 

Kind regards, Gena


Hi I'm trying to implement MVCResourceCommand for calendar portlet but its not working  

Please let me know in case i missed something to add

 

 

 

@Component(
	immediate = true,
	property = {"javax.portlet.name=com_liferay_calendar_web_portlet_CalendarPortlet",
			 "mvc.command.name=calendarRenderingRules"
			},
			service = MVCResourceCommand.class
			)
public class CalendarPortletAction implements MVCResourceCommand{

	@Override
    public boolean serveResource (ResourceRequest resourceRequest, ResourceResponse resourceResponse)
            throws PortletException {
        System.out.println (">>>>>>>>>>>>> before");

        String resourceID = GetterUtil.getString (resourceRequest.getResourceID ());
        String cmd = ParamUtil.getString (resourceRequest, "autoComplete");
        String searchKey = ParamUtil.getString (resourceRequest, "searchKey");
        System.out.println (">>> after");
        return mvcResourceCommand.serveResource (resourceRequest, resourceResponse);
    }

    protected MVCResourceCommand mvcResourceCommand;
}
Ghenadii Batalski, modificado hace 5 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

New Member Mensajes: 7 Fecha de incorporación: 31/07/17 Mensajes recientes

Hello, does your ResourceCommad called at all? If not, what is your patching approach? You should create for your additional command a new osgi bundle. If you try to add new command to a fragment, it won't work.

 

Regards,  Gena

thumbnail
David H Nebinger, modificado hace 5 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes

This code has issues.

First, if you are trying to override an existing MVCResourceCommand (that's not completely clear), it's missing a service ranking override.

Second, the mvcResourceCommand member is never initialized, so it is null. Even if the code did get called, you'd end up with an NPE as a result.

kamdon jacaden, modificado hace 4 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

New Member Mensajes: 2 Fecha de incorporación: 24/10/19 Mensajes recientes
[quote=]You should create for your additional command a new osgi bundle. If you try to add new command to a fragment, it won't work.​​​​​​​Vishal Shah
Hi,

In Liferay DXP, How can I add the custom MVCResourceCommand in Out-of-Box portlet (Ex. Search Portlet)?
thumbnail
Andrew Jardine, modificado hace 4 años.

RE: How can I add the custom MVC ResourceCommand in Out-of-Box portlet

Liferay Legend Mensajes: 2416 Fecha de incorporación: 22/12/10 Mensajes recientes
Simple.
  1.  Create a new module (or use an existing one if you want)
  2. Create your new MVCResourceCommand class 
  3. In the @Component annotation, make sure you specify the javax.portlet.name= the portletId for the Search Portlet -- or whatever portlet you want to associate the resource command with

That's it.  I've used this trick many times. The only thing you need to make sure, which of course has always been the case -- including pre-7.x, is that you specify the correct portletId and a layoutId that has the portlet. If you have embedded the search portlet in your theme, then you can basically hit the resource command from any page where you theme is applied.