From Liferay 6 portlets need to build using plugin SDK. Download PluginSDK, which is a separate download along with Liferay source.
Here are the steps to build portlets in Liferay 6.
1. Download Liferay-tomcat bundle, liferay-plugins-sdk-6.0.5.zip and liferay-portal-src-6.0.5.zip.
2. Extract liferay-tomcat bundle into "liferay-portal" dir. I am extracting this files into e:/liferay-portal-6.0.5
3. extract liferay-portal-src-6.0.5 and liferay-plugins-sdk-6.0.5.zip into this folder.
4. go to liferay-plugins-sdk-6.0.5 and edit build.properties file
app.server.dir=e:/liferay-portal-6.0.5/tomcat-6.0.29
5. go to folder E:\liferay-portal-6.0.5\liferay-plugins-sdk-6.0.5\portlets and execure
create.bat myPortlet "Myportlet"
6. A folder called "MyPortlet-portlet" would be created
7. configure eclipse project settings for this directory by going into this directoy and executing
ant setup-eclipse
8. Import this folder into eclipse
9. Execute ant deploy.
And your running tomcat instance should hot deploy it.
The portlet created is instance of MVCPortlet and should be available in "Samples" section.
Now the tricky part. The eclipse portlet project does not have any place to add java source files. Infact the eclipse project created is not a Java project and .classpath file is not available as well. You need to convert this project into Java project if you want to customize your portlet further.
1. Convert the project created by setup-eclipse command into Java Project.
Edit thre .project file. This is an XML file.
Replace buildSpec and natures of .project file with the following snippet
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
add a .classpath file and with the following snippet
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>
Close and reopen the project.
2. add source
create a source folder called "src"
3. Configure the bin-output to WEB-INF/classes
go to project-properties-->Source-->DefaultOutput folder
click the browse button and set this as WEB-INF classes.
Refresh the project.
4. Add required jars in the class path.
The following jars from E:\liferay-portal-6.0.5\portal-src\util** folders and TOMCAT/lib/ext folders have to be added.
util-bridges.jar, util-java.jar, util-taglibs.jar, portlet.jar, portal-service.jar.
5. Create a class called MyPortlet.java
Override the doView method. You can put some SOP and set some attributes to renderRequest.
6. Update the portlet-class to MyPortlet in portlet.xml.
7. Now you can read and display the value set in renderRequest in view.jsp.
Tuesday, March 29, 2011
Monday, March 21, 2011
Generating PDF's using Apache FOP
I recently used FOP to generate PDF invoices. The site has done a great job in documenting it. But I feel that there is some confusion regarding where to start.
Here is a simplified tutorial for FOP.
1. Download Apache FOP binaries.
2. FOP uses XSL-FO as input format. So XSL-FO tutorial from w3schools xsl-fo is required reading.
3. Add the binaries in the java project. If you are using Maven it would be.
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>1.0</version>
</dependency>
4. Construct the following xsl-fo file.
<fo:page-sequence master-reference="invoiceContent">
<fo:flow flow-name="xsl-region-body">
<fo:table >
<fo:table-column column-width="3.5cm"/>
<fo:table-column column-width="15cm"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block text-align="start" >
<fo:inline><fo:external-graphic src="http://twitter.com/twitter.gif" /></fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block margin-top="5mm" text-align="start" font-family="sans-serif" font-weight="bold" font-size="14pt" color="#990000" >
<fo:inline ><xsl:value-of select="fromCompany"/></fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
5. Notice that in the above code we are using xsl transformations.
6. The following snippet of code would generate the PDF for you.
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent,out);
// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(
xsltfile)); // identity transformer
// Setup input stream
Source src = invoiceViewVO.getSourceForProjectTeam();
// Resulting SAX events (the generated FO) must be piped through to
// FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
In the above code snippets concentrate on the bolded text.
<xsl:value-of select="fromCompany"/>
The xsl-value would hunt for the element "fromCompany" in an xml input and replace it in the XSL before generating the PDF.
We can also provide this value from a Java object. The following line of code substitutes java object for a XML source.
Source src = invoiceViewVO.getSourceForProjectTeam();
In getSourceForProjectTeam() method we are returning a proxy which overides the SAX events for "value-of" and provides the requested element from java object.
For better understanding how this events are overrided
see the following files
1. AbstractObjectReader, 2, EasyGenerationContentHandlerProxy and 3. ExampleObj2PDF
I recommend that you go through the xsl-fo tutorial in details if you want the harness the full power of ApacheFO.
Here is a simplified tutorial for FOP.
1. Download Apache FOP binaries.
2. FOP uses XSL-FO as input format. So XSL-FO tutorial from w3schools xsl-fo is required reading.
3. Add the binaries in the java project. If you are using Maven it would be.
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>1.0</version>
</dependency>
4. Construct the following xsl-fo file.
<fo:page-sequence master-reference="invoiceContent">
<fo:flow flow-name="xsl-region-body">
<fo:table >
<fo:table-column column-width="3.5cm"/>
<fo:table-column column-width="15cm"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block text-align="start" >
<fo:inline><fo:external-graphic src="http://twitter.com/twitter.gif" /></fo:inline>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block margin-top="5mm" text-align="start" font-family="sans-serif" font-weight="bold" font-size="14pt" color="#990000" >
<fo:inline ><xsl:value-of select="fromCompany"/></fo:inline>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
5. Notice that in the above code we are using xsl transformations.
6. The following snippet of code would generate the PDF for you.
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent,out);
// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(
xsltfile)); // identity transformer
// Setup input stream
Source src = invoiceViewVO.getSourceForProjectTeam();
// Resulting SAX events (the generated FO) must be piped through to
// FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
In the above code snippets concentrate on the bolded text.
<xsl:value-of select="fromCompany"/>
The xsl-value would hunt for the element "fromCompany" in an xml input and replace it in the XSL before generating the PDF.
We can also provide this value from a Java object. The following line of code substitutes java object for a XML source.
Source src = invoiceViewVO.getSourceForProjectTeam();
In getSourceForProjectTeam() method we are returning a proxy which overides the SAX events for "value-of" and provides the requested element from java object.
For better understanding how this events are overrided
see the following files
1. AbstractObjectReader, 2, EasyGenerationContentHandlerProxy and 3. ExampleObj2PDF
I recommend that you go through the xsl-fo tutorial in details if you want the harness the full power of ApacheFO.
Saturday, March 19, 2011
Quick and Simple- Wider blog content area
Hmm, I was feeling that the width of the content area was too narrow.
Could not live with it and also did not feel that full template customization is required.
So there is what I did
1. Go to blogger site.
2. Select your blog
3. Click on templates
4. Edit HTML
5. Modify #content's width to 940 from 640.
6. Remove the rounded corners from #main and #main2
and presto you have a wider blog :)
Could not live with it and also did not feel that full template customization is required.
So there is what I did
1. Go to blogger site.
2. Select your blog
3. Click on templates
4. Edit HTML
5. Modify #content's width to 940 from 640.
6. Remove the rounded corners from #main and #main2
and presto you have a wider blog :)
Friday, March 18, 2011
Eclipse WTP- Maven - Sample Web App
Using WTP and Maven we can easily create web applications.
Install m2 plugin from sonatype. You can search for it in eclipse market place.
Step 1: Create a new Maven Project. "HelloApp"
Step 2: Configure the created projected for WTP by running the following command.
eclipse:eclipse -Dwtpversion=1.5

Step3: Create a tomcat server instance.
Step4: Add the "HelloApp" project to tomcat server.
Step5: Add spring mvc componets to pom.xml.
Step6: Start the server.
Some times the WTP is buggy and the server may give the following exceptions.
SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
This exception means that the required jar containing ContextLoaderListener class is not available in the tomcat webapps folder. Publishing to tomcat should take copy the required jar's into WEB-INF/lib, but if it does not happen check the deployment assembly.
Deployment Assembly Configuration
In this case we need to go set the deployment assembly manually.
1. Go to project settings. Check your deployment assembly.

2. Add the entries from Java build path.

3. Final Deployment setup.

Take care not to keep servlet-api related jars in deployment assembly as they are already present with tomcat.
Install m2 plugin from sonatype. You can search for it in eclipse market place.
Step 1: Create a new Maven Project. "HelloApp"
Step 2: Configure the created projected for WTP by running the following command.
eclipse:eclipse -Dwtpversion=1.5

Step3: Create a tomcat server instance.
Step4: Add the "HelloApp" project to tomcat server.
Step5: Add spring mvc componets to pom.xml.
Step6: Start the server.
Some times the WTP is buggy and the server may give the following exceptions.
SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361)
This exception means that the required jar containing ContextLoaderListener class is not available in the tomcat webapps folder. Publishing to tomcat should take copy the required jar's into WEB-INF/lib, but if it does not happen check the deployment assembly.
Deployment Assembly Configuration
In this case we need to go set the deployment assembly manually.
1. Go to project settings. Check your deployment assembly.

2. Add the entries from Java build path.

3. Final Deployment setup.

Take care not to keep servlet-api related jars in deployment assembly as they are already present with tomcat.
Subscribe to:
Comments (Atom)