Tuesday, June 15, 2010

Struts Action Portlet in Plugin SDK

Create Struts Action Portlet in Plugin Enviroinment

Step 1.

Go to Command Prompt inside Plugin: Plugin>Portlets>

Execute the create.bat file with your portlet name as follows:

project\plugin\portlets>create.bat sample-struts-plugin "Sample Struts Plugin Portlet"

Step 2:

Folder with name sample-struts-plugin-portlet will be created in ur portlets folder. Verify it.

Go to ur portlet folder and edit the liferay-portlet.xml file by adding the following after

\portlets\sample-struts-plugin-portlet\docroot\WEB-INF\liferay-portlet.xml

sample/struts/plugin

Step 3:

Go to ur portlet folder and edit the portlet.xml file by changing the following tags..

(\portlets\sample-struts-plugin-portlet\docroot\WEB-INF\portlet.xml)

Replace ,

com.sample.jsp.portlet.JSPPortlet

with

com.liferay.portlet.StrutsPortlet

---------------------------------

And replace,

view-jsp

/view.jsp

with

view-action

/sample/struts/plugin/view

---------------------------------

and ADD,

text/html

view

Step 4:

Go to ur WEB-INF folder & create struts-config.xml like this

( \portlets\sample-struts-plugin-portlet\docroot\WEB-INF\struts-config.xml)

Step 5:

Go to ur WEB-INF folder & create tiles-defs.xml like this

(\portlets\sample-struts-plugin-portlet\docroot\WEB-INF\tiles-defs.xml)

Step 6:

Goto ur WEB-INF\src\com\xyz\action\ folder & create SampleStrutsAction.java insie:

(\portlets\sample-struts-plugin-portlet\docroot\WEB-INF\src\com\xyz\action\ )

package com.xyz.action;

import javax.portlet.PortletConfig;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import com.liferay.portal.struts.PortletAction;

public class SampleStrutsAction extends PortletAction{

public ActionForward render(ActionMapping mapping, ActionForm form,

PortletConfig portletConfig, RenderRequest renderRequest,

RenderResponse renderResponse) throws Exception {

System.out.println("SampleStrutsAction render()..");

return mapping.findForward("portlet.sample.struts.view");

}

}

Step 7:

Goto ur WEB-INF folder & edit web.xml :

( \portlets\sample-struts-plugin-portlet\docroot\WEB-INF\web.xml )

com.liferay.portal.kernel.spring.context.PortletContextLoaderListener

PortletActionServlet

com.liferay.portal.struts.PortletActionServlet

config

/WEB-INF/struts-config.xml

1

PortletActionServlet

/portlet_action/*

Step 8:

Go to ur docroot folder & create META-INF\context.xml like this

(\portlets\sample-struts-plugin-portlet\docroot\META-INF\context.xml )

loaderClass="com.liferay.support.tomcat.loader.PortalClassLoader"

/>

Step 9:

create view.jsp file inside :

(\sample-struts-plugin-portlet\docroot\html\struts\jsp\view.jsp)

File Structure Tree View for Plugin Portlet:

Tree View of Struts Action Portlet  in SDK


Ref: http://www.liferay.com/community/wiki/-/wiki/Main/Struts+Action+Portlet+in+Plugin+SDK

Tuesday, November 24, 2009

Inter Portlet Communication (IPC) in Liferay - Blog - Liferay

Inter Portlet Communication (IPC) in Liferay

A common question and topic when I do training or work on support cases, is, "What is the right way to do inter portlet communication in Liferay?"

Well, there is no "right way" to do this. At least not yet. This is because IPC was left out of JSR-168. In other words, there is no standard API. As for JSR-286, there will be a IPC standard that will be an event system. IPC is on the Liferay roadmap as well.

For the time being, here are three ways in which IPC can be implemented:

Method 1) The first is to use the request attributes to pass messages between portlets. You can also use session attributes for this as well.

Method 2) We provide a proprietary portlet url tag that allows you to target one portlet to another (cross-linking). It looks something like this:

Method 3) Use our AJAX toolkit to inject content from one portlet to another. See http://wiki.liferay.com/index.php/Ajax_Toolkit for more information. You can see how you can use this in conjunction with portlet urls to inject content into a div layer.

The most recommended approach is to use (Method 1) from above, using request or session attributes.

In order for this to work you must have the portlet calling request.setAttribute() and it needs to be doing it within processAction(), so that this executes before the other portlets call render().

Once you do this IPC is complete, the other portlets can pick up the message using renderRequest.getAttribute().

AJAX vs. NON-AJAX

You will have to use a non-ajax approach if you want it to be more flexible. So basically, the trade-off is:

1) non-ajax approach is more flexible

2) ajax approach requires hardcoding of the portlet name in the JSP

Remember, there is no standard or API yet, so we can only give hints on how to implement!


Monday, November 23, 2009

Access to Liferay services in Velocity

Access to Liferay services in Velocity

"Access to Liferay services in Velocity
Tags: 5.1.0 customization journal template service locator velocity

Table of Contents
* 1 FindService
* 2 FindExceptionSafeService
* 3 Service Locator doesn't work for me???
* 4 Related Articles

In your themes you sometimes want to invoke Liferay services. Liferay provides a utility to obtain references to services; $serviceLocator.
FindService

For example, suppose you need layoutLocalService:

#set($layoutLocalService = $serviceLocator.findService('com.liferay.portal.service.LayoutLocalService'))

#set($xyz = $layoutLocalService.getFriendlyURLLayout($layout.getOwnerId(), '/home'))


The services obtained from the findService method are the raw services and sometimes they can be difficult to work with because Velocity does not provide an exception handling mechanism.


FindExceptionSafeService

To handle this issue, more recent versions of Liferay (5.1.0+) provide a second method, findExceptionSafeService which wraps the underlying service with a proxy which catches any exceptions and simply returns null.

For example, suppose you want to see if some userId refers to an actual user:

#set($userLocalService = $serviceLocator.findExceptionSafeService("com.liferay.portal.service.UserLocalService"))
The following method would typically crash the VM being processed, because it throws a NoSuchUserException if the user cannot be located.
#set($user = $userLocalService.getUserById(
$getterUtil.getLong("12345"))

Service Locator doesn't work for me???

ServiceLocator is a feature which unlocks a lot of power. This power can inadvertently fall into the wrong hands if for example Journal Template creation is granted to less trusted users. Therefore to prevent abuse, or simply to protect less aware portal admins, the feature is disabled by default for all Journal VM contexts.

To enable it, edit the value of journal.template.velocity.restricted.variables in portal.properties.


Friday, November 20, 2009

What are the fundamental differences between Struts and JSF

What are the fundamental differences between Struts and JSF:

"Routing around the hyperbole and actually trying to answer the question...

The key differences between the two are the base paradigms that underly each platform.

Specifically, JSF is a 'component' framework whereas Struts is an 'action' framework.

What does that mean?

In a component framework, artifacts that are rendered on the page are initially developed as individual components, much like in modern GUI 'fat client' libraries. You have components, they have events, and your code is written to work with those events against the components.

Most of the time, in mainstream development, your code is pretty much ignorant of the HTTP request cycle and processing.

Struts (both 1 and 2) are action frameworks. In essence they give you the ability to map URLs to activities and code on the back end. Here, the layout and workflow tends to be more page oriented. As a developer you tend to interact with the HTTP request cycle directly, though Struts 2 helps isolate at least the binding of the request data to the action implementation classes.

Action frameworks tend to be much 'thinner' in how they stand between your code and the raw HTTP request compared to component frameworks.

For those people just cutting their teeth on web development, the component"