Create Struts Action Portlet in Plugin Enviroinment

September 10, 2009 by Gnaniyar Zubair

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 <icon/>

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

<struts-path>sample/struts/plugin</struts-path>

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 ,

<portlet-class>com.sample.jsp.portlet.JSPPortlet</portlet-class>

with

<portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>

————————————————————————

And replace,

<init-param>

<name>view-jsp</name>

<value>/view.jsp</value>

</init-param>

with

<init-param>

<name>view-action</name>

<value>/sample/struts/plugin/view</value>

</init-param>

————————————————————————

and ADD,

<supports>

<mime-type>text/html</mime-type>

<portlet-mode>view</portlet-mode>

</supports>


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)

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE struts-config PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 1.2//EN” “http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd”>

<struts-config>

<action-mappings>

<action path=”/sample/struts/plugin/view” type=”com.xyz.action.SampleStrutsAction” >

<forward name=”portlet.sample.struts.view” path=”portlet.sample.struts.view” />

</action>

</action-mappings>

<controller processorClass=”org.apache.struts.tiles.TilesRequestProcessor”></controller>

<message-resources parameter=”content.test.Language”></message-resources>

<plug-in className=”org.apache.struts.tiles.TilesPlugin”>

<set-property property=”definitions-config” value=”/WEB-INF/tiles-defs.xml”></set-property>

<set-property property=”moduleAware” value=”true”></set-property>

<set-property property=”definitions-parser-validate” value=”true”></set-property>

</plug-in>

</struts-config>

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)

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE tiles-definitions PUBLIC “-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN” “http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd”>

<tiles-definitions>

<!– Sample Struts Plugin –>

<definition name=”portlet.sample.struts.view” path=”/struts/jsp/view.jsp”></definition>

</tiles-definitions>

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 )

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd”>

<web-app>

<listener>

<listener-class>com.liferay.portal.kernel.spring.context.PortletContextLoaderListener</listener-class>

</listener>

<servlet>

<servlet-name>PortletActionServlet</servlet-name>

<servlet-class>com.liferay.portal.struts.PortletActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>PortletActionServlet</servlet-name>

<url-pattern>/portlet_action/*</url-pattern>

</servlet-mapping>

</web-app>

Step 8:

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

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

<?xml version=”1.0″ encoding=”UTF-8″?>

<Context>

<Loader

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

/>

</Context>

Step 9:

create view.jsp file inside :

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

File Structure:

file structure for plugin

Javascript To accept only alphapets

June 29, 2009 by Gnaniyar Zubair

This javascript is used to accept only alphapets value in text box.

Coding:

<input onkeyup=”validateAlphapets(this, ”)” ruleset=”[^a-z]” />

<script type=”text/javascript”>

function validateAlphapets(element, replacement) {

if(! element)

element = window.event.srcElement;

element.value = element.value.replace(new   RegExp(element.getAttribute(‘ruleset’), ‘gi’), replacement);

}

</script>

Javascript To Accept Only Numeric

June 29, 2009 by Gnaniyar Zubair

This javascript is used to accept only numeric value in text box.

Coding :

<input type=”text “   onKeyPress=”return alfanumaricVal(event)”  name=”amount” />

<script type=”text/javascript”>

function alfanumaricVal(e) {

var key;

var keychar;

if (window.event)

key = window.event.keyCode;

else if (e)

key = e.which;

else

return true;

keychar = String.fromCharCode(key);

keychar = keychar.toLowerCase();

// control keys

if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )

return true;

// numbers

else if (((“0123456789″).indexOf(keychar) > -1))

return true;

else

return false;

}

</script>

Struts Portlet with DataBase interaction

June 29, 2009 by Gnaniyar Zubair

The goal of this post is to add Database interaction to the Struts Portlet.

Steps  Involved:

1. Create Folder structure in ext-impl and ext-web

2. Create your table structure in service.xml

3. Generate Service and Persistence Layer Classes with Ant’s Build-Service

4.  Copy auto-generated MySQL table  into database.

5.  Create the Struts Action Portlet

6.  Update Service Layer Files

7.   JSP / Java action file updation.

1. Folder Structure:

Java Fiels >>>>>>>  ext-impl/src/com/testing/portlet/company

Jsp Files  >>>>>>>   ext-web/docroot/html/testing/company

2.  Service.xml
(create this file under “/ext/ext-impl/src/com/testing/portlet/company“)

<?xml version=”1.0″?>
<!DOCTYPE service-builder PUBLIC “-//Liferay//DTD Service Builder 5.2.0//EN” “http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd”>

<service-builder package-path=”com.testing.portlet.company”>
<namespace>MyCompany</namespace>

<entity name=”MyCompany”   local-service=”true”  remote-service=”true”   table=”my_company”>
<column name=”companyId” type=”long” primary=”true” />

<!– basic Info –>

<column name=”companyName” type=”String” />

<column name=”address” type=”String” />

<column name=”startDate” type=”Date” />

<column name=”annualIncome” type=”float” />

<column name=”stockQty” type=”double” />

<column name=”active”  type=”int” />

<!– Finder Method for companyName  Info –>
<finder name=”Name” return-type=”FAIRDEALCompany”>
<finder-column name=”companyName” />
</finder>

<reference package-path=”com.liferay.counter” entity=”Counter” />

</entity>

<exceptions>
<exception>NoSuchMyCompany</exception>
</exceptions>
</service-builder>

3. Run build-service with “service.xml”  as argument

a. go to   “/ext/ext-impl”

b. execute   “ant build-service -Dservice.file=src/com/testing/portlet/company/service.xml

c. make sure the build is successful

d. confirm all the service layer files are generated as per the Spring/Hibernate framework. check under

ext/ext-impl/src/com/testing/portlet/company
ext/ext-service/src/com/testing/portlet/company

4.   Copy the table  “my_company.sql”

copy the my_company.sql file from ext/sql/portal_tables and past it to database “lportal“.

To run the SQL directly, you’ve to go to mysql prompt by giving   “mysql -u root lportal

create table my_company (
companyId LONG not null primary key,
companyName VARCHAR(75) null,
address  VARCHAR(75),
startDate DATE null,
annualIncome DOUBLE,
stockQty DOUBLE,
active_ INTEGER,
);

5. Create Struts Action Portlet

5.1 portlet-ext.xml

<portlet>
<portlet-name>MyCompany</portlet-name>
<display-name>My Company</display-name>
<portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>
<init-param>
<name>view-action</name>
<value>/testing/company/view</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>

5.2. liferay-portlet-ext.xml

<portlet>
<portlet-name>MyCompany</portlet-name>
<struts-path>testing/company</struts-path>
<use-default-template>false</use-default-template>
</portlet>

5.3. liferay-display.xml

<portlet id=”MyCompany” />

inside,

<category name=”category.MyCompany“>

5.4. struts-config.xml

<action path=”/testing/company/process“   type=”com.testing.portlet.company.action.ProcessMyCompanyAction“>
<forward name=”testing.company.view“    path=”testing.company.view” />
</action>

5.5. tiles-defs.xml

<definition name=”testing.company.view” extends=”portlet”>
<put name=”portlet_content” value=”/testing/company/view.jsp” />
</definition>

6.  Update Service Layer  Files:

add this method in MyCompanyLocalServiceImpl.java which is autogenerated ( in this path: com/testing/portlet/company/service/)

public MyCompany getCompanyName(String companyName) throws SystemException, PortalException{
return MyCompanyUtil.findByName(companyName);
}

import these files:

import com.testing.portlet.company.service.persistence.MyCompanyUtil;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;

then give ant build-service inside ext-impl

7.  Update / Create these Jsp / Java Files:

(jsp files path: ext-web/docroot/html/testing/company)

a. view.jsp

<%@ include file=”/html/testing/company/init.jsp” %>

<div id=”MYCOMPANY”>
<%
String PAGE = (String) request.getAttribute(“PAGE”);
System.out.println(“PAGE value is >>>>” + PAGE);

%>

<div id=”MYCOMPANY_CONTENT”>
<jsp:include page=”<%= PAGE %>” />
</div>
</div>

<% SessionMessages.clear(request); %>

a. add_company.jsp

<%@ include file=”/html/testing/company/init.jsp” %>

<form action=”" method=”post” name=”<portlet:namespace />fm”    id=”<portlet:namespace />fm_id”>

<table width=”100%”> <tr>
<%if(SessionMessages.contains(session,”Added”)){%>
<td height=”15px”>
<div>
<liferay-ui:message key=”Record Added Successfully” />
</div>
</td>
<% } %>

</tr>

</table>

<table width=”100%”   style=”border:1px solid lightblue”    align=”center”>

<tr >
<td> Company Name: <%=star%></td>
<td colspan=”3″> <input style=”width:60%” type=”text” id=”<portlet:namespace />companyName”   name=”<portlet:namespace />companyName“> </td>
</tr>

<tr class=’portlet-section-alternate results-row alt’>
<td> Address: </td>
<td colspan=”7″> <input style=”width:60%” type=”text” id=”<portlet:namespace />address”   name=”<portlet:namespace />address”> </td>
</tr>

<tr class=’portlet-section-alternate results-row alt’>
<td> e Start Date: </td>
<td colspan=”7″> <input style=”width:60%” type=”text”   id=”<portlet:namespace />startDate”   name=”<portlet:namespace />startDate“> </td>
</tr>

<tr class=’portlet-section-alternate results-row alt’>
<td> Annual Income: </td>
<td colspan=”7″> <input style=”width:60%” type=”text” id=”<portlet:namespace />annualIncome”   name=”<portlet:namespace />annualIncome“> </td>
</tr>

<tr class=’portlet-section-alternate results-row alt’>
<td> Stock Qty: </td>
<td colspan=”7″> <input style=”width:60%” type=”text”  id=”<portlet:namespace />stockQty”   name=”<portlet:namespace />stockQty“> </td>
</tr>

<tr class=’portlet-section-alternate results-row alt’>
<td> Active : </td>
<td colspan=”7″> <input style=”width:60%” type=”text” id=”<portlet:namespace />active”   name=”<portlet:namespace />active“> </td>
</tr>

<tr>

<td colspan=”2″ align=”center”>
<input type=”submit” value=”Submit”    id=”<portlet:namespace />submit“/> &nbsp; &nbsp;

<input type=”button”  value=”Cancel” onclick=”addCompany()” id=”<portlet:namespace />Cancel” >

</td>

</tr>

</table>

b. company.jsp ( javascript file)

<script language=”Javascript”>
jQuery(document).ready(function() {
jQuery.metadata.setType(“attr”, “validate”);
var validator = jQuery(“#<portlet:namespace/>fm_id”).validate({
rules: {
<portlet:namespace/>companyName:{
required:true,
maxlength:75
}
},
messages: {
<portlet:namespace/>companyName:”Enter Valid Company Name “,
},
errorPlacement: function(error, element) {
error.appendTo(element.parent());
},
submitHandler: function() {
SubmitForm(document.<portlet:namespace />fm, ‘<portlet:actionURL windowState=”<%= WindowState.NORMAL.toString() %>”><portlet:param name=”struts_action” value=”/testing/company/process“/><portlet:param name=”CMD” value=”add_company” /></portlet:actionURL>’, ‘MYCOMPANY‘, false);
},
success: function(label) {
label.html(“&nbsp;”).addClass(“checked”);
}
});
});
</script>

c. init.jsp


<%@ include file=”/html/common/init.jsp” %>

<%– Add these jquery plugins –%>

<script src=”/html/js/jquery/jquery.metadata.js” type=”text/javascript”></script>

<script src=”/html/js/jquery/jquery.delegate.js” type=”text/javascript”></script>

<script src=”/html/js/jquery/jquery.validate.js” type=”text/javascript”></script>
<script src=”/html/js/jquery/jquery.js” type=”text/javascript”></script>

<%@ include file=”/html/testing/company/company.jsp” %>

<%
PortletPreferences prefs = renderRequest.getPreferences();
PortletSession pses = renderRequest.getPortletSession();
Calendar cal = Calendar.getInstance();
themeDisplay.setIncludeCalendarJs(true);
%>

(java files path: ext-impl/src/com/testing/portlet/company/action/ProcessMyCompanyAction.java)

d. ProcessMyCompanyAction.java

import these files :

import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portlet.ActionRequestImpl;
import com.liferay.portlet.ActionResponseImpl;

import com.liferay.counter.service.CounterServiceUtil;

import com.liferay.portal.kernel.util.Validator

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletSession;
import javax.portlet.PortletURL;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.testing.portlet.company.model.MyCompany;
import com.testing.portlet.company.service.MyCompanyLocalServiceUtil;
import com.testing.portlet.company.service.MyCompanyServiceUtil;

public class ProcessMyCompanyAction extends PortletAction {
private static Log log = LogFactory.getLog(ProcessMyCompanyAction.class);

public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest req,
ActionResponse res) throws Exception {

PortletURL url = ((ActionResponseImpl) res).createRenderURL();
url.setParameter(“struts_action”, “/testing/company/view”);
String cmd = ParamUtil.getString(req,”CMD”);
HttpServletRequest httpReq = ((ActionRequestImpl) req).getHttpServletRequest();
PortletSession ses = req.getPortletSession();
HttpSession httpSession = httpReq.getSession();

log.info(“ProcessMyCompanyAction CMD Value >> “+cmd);
Date date = new Date();

MyCompany company = null;
long companyId = 0;
// company Name Duplicate Verification
String companyName = ParamUtil.getString(req,”companyName”);
String findCompanyName = “”;
try {
company = MyCompanyLocalServiceUtil.getCompanyName(companyName);
findCompanyName = company.getCompanyName();
}catch(Exception e) {
log.info(“CompanyName Not EXIST”);
}
log.info(“companyName” + companyName);
log.info(“findCompanyName” + findCompanyName);

if (Validator.isNotNull(cmd) && cmd.equalsIgnoreCase(“add_company“)) {

if (Validator.isNull(findCompanyName)) {
// set all fields into database
companyId = CounterServiceUtil.increment();
company = MyCompanyLocalServiceUtil.createMyCompany(companyId);
company.setCompanyName(ParamUtil.getString(req,”companyName”));
company.setActive(ParamUtil.getInteger(req,”active”));
company.setAddress(ParamUtil.getString(req,”address”));
company.setAnnualIncome(ParamUtil.getDouble(req,”annualIncome”));
company.setStockQty(ParamUtil.getString(req,”stockQty”));
company.setStartDate(ParamUtil.getString(req,”startDate”));

MyCompanyLocalServiceUtil.updateMyCompany(company);
SessionMessages.add(httpReq, “Added”);
} else {
SessionMessages.add(httpReq, “DuplicateCompanyName”);
}

res.sendRedirect(url.toString());
}

}

e. ViewMyCompanyAction.java

public class ViewMyCompanyAction extends PortletAction{

private static Log log = LogFactory.getLog(ViewMyCompanyAction.class);

public ActionForward render(
ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
RenderRequest req, RenderResponse res)
throws Exception {

HttpServletRequest httpReq = ((RenderRequestImpl) req).getHttpServletRequest();
PortletSession ses = req.getPortletSession();
HttpSession httpSession = httpReq.getSession();
String cmd = ParamUtil.getString(req,”CMD”);
req.setAttribute(“PAGE”, “/html/testing/company/add_company.jsp”);
return mapping.findForward(“testing.portlet.company.view”);

}
}

import these files:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.liferay.portlet.RenderRequestImpl;

import javax.portlet.PortletConfig;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

Struts Portlet with Action in Liferay 5.2.x

June 29, 2009 by Gnaniyar Zubair

Here i  am pointing out  Struts Portlet with Action classes.

To create this portlet, just take the same procedure from my last post  “Simple Struts Portlet in Liferay 5.2.x”

Everything is same as i mentioned in last post but you need to update the below  files only.

1. struts-config.xml

(add a new actionfile  ” AddDetailsAction.java” in this path : com/testing/portlet/mystrutsportlet/action)

<action path=”/testing/mystrutsportlet/add_details” type=”com.testing.portlet.mystrutsportlet.action.AddDetailsAction“>
<forward name=”testing.mystrutsportlet.view” path=”testing.mystrutsportlet.view” />
<forward name=”testing.mystrutsportlet. success” path=”testing.mystrutsportlet. success” />
<forward name=”testing.mystrutsportlet.failure” path=”testing.mystrutsportlet.failure” />
</action>

2. tiles-defs.xml

(add two more definitions)

<definition name=testing.mystrutsportlet.view" extends="portlet">
 <put name="portlet_content" value="/testing/mystrutsportlet/view.jsp" />
 </definition>

<definition name=testing.mystrutsportlet.success" extends="portlet">
 <put name="portlet_content" value="/testing/mystrutsportlet/success.jsp" />
 </definition>
<definition name=testing.mystrutsportlet.failure" extends="portlet">

<put name="portlet_content" value="/testing/mystrutsportlet/failure.jsp" />
</definition>

3. init.jsp

<%@ include file=”/html/portlet/init.jsp” %>

4. view.jsp

(modify this file)

<%@ include file=”/html/portlet/testing/mystrutsportlet/init.jsp” %>

<%
PortletURL actionURL = renderResponse.createActionURL();
actionURL.setWindowState(WindowState.MAXIMIZED);
actionURL.setParameter(“struts_action”, “/testing/mystrutsportlet/add_details“);
%>

Add a Details :
<br/>

<form action=“<%= actionURL.toString() %>“  method=”POST“  name=”<portlet:namespace />fm”>

Name:  <input type=”text”  name=”<portlet:namespace />name” /><br/><br/>

<input type=”submit”  value=”Submit”/>
</form>

5. success.jsp

<%@ include file=”/html/portlet/testing/mystrutsportlet/init.jsp” %>
<%
String name = request.getParameter(“name”);
%>

<table align=”center” cellspacing=”10″ cellpadding=”3″>
<tr>
<td style=”font-weight:bold”>Name : </td>
<td><%= name%></td>
</tr>
</table>

6. failure.jsp

<%@ include file=”/html/portlet/testing/mystrutsportlet/init.jsp” %>
<font color=”red”>Error in page…</font>

7. AddDetailsAction.java

(location: ext/ext-impl/src/com/testing/mystrutsportlet/action/AddDetailsAction.java)

package com.testing.mystrutsportlet.action;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
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;
import com.liferay.portal.kernel.util.Validator;

public class AddDetailsAction extends PortletAction {

public void processAction(
ActionMapping mapping, ActionForm form, PortletConfig config,
ActionRequest req, ActionResponse res)
throws Exception {

String name= req.getParameter(“name”);

if (Validator.isNull(name)) {
setForward(req, “testing.mystrutsportlet. failure“);
} else {
setForward(req, “testing.mystrutsportlet. success“);
}
}

public ActionForward render(ActionMapping mapping, ActionForm form,
PortletConfig config, RenderRequest req, RenderResponse res)
throws Exception {
if (getForward(req) != null && !getForward(req).equals(“”)) {
return mapping.findForward(getForward(req));
} else {
return mapping.findForward(“testing.mystrutsportlet. view“);
}
}
}

You have successfully created Struts Portlet with Action class.

Simple Struts Portlet in Liferay 5.2.x

June 29, 2009 by Gnaniyar Zubair

Struts portlets are based on the Struts framework, which is an implementation of the Model-View-Controller (MVC) architecture.

The MVC architecture provides a model for separating the different components and roles of the application logic. This development framework helps you create portlets that are easier to maintain over time.

You have to follow the same procedure and file structures as i mentioned in my last post to create the Struts portlet in Liferay.

I. Development

1.1 portlet-ext.xml

<portlet>
<portlet-name>MYSTRUTSPORTLET</portlet-name>
<display-name>My Struts Portlet</display-name>
<portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>
<init-param>
<name>view-action</name>
<value>/testing/mystrutsportlet/view</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>

2.2. liferay-portlet-ext.xml

<portlet>
<portlet-name>MYSTRUTSPORTLET</portlet-name>
<struts-path>testing/mystrutsportlet</struts-path>
<use-default-template>false</use-default-template>
</portlet>

2.3. liferay-display.xml

<portlet id=”MYSTRUTSPORTLET” />

inside,

<category name=”category.example“>

2.4. struts-config.xml

<action path=”/testing/mystrutsportlet/view” forward=”portlet.testing.mystrutsportet.view” />

2.5. tiles-defs.xml

<definition name=”portlet.testing.mystrutsportlet.view“  extends=”portlet”>

<put name="portlet_content" value="/portlet/testing/mystrutsportlet/view.jsp" />

</definition>

(create the following two jsp files under “/ext/ext-web/docroot/html/portlet/testing/mystrutsportlet“)

2.6. init.jsp

<%@ include file=”/html/portlet/init.jsp” %>
<p>Add commonly used variables and declarations here!</p>

2.7. view.jsp

<%@ include file=”/html/portlet/testing/mystrutsportlet/init.jsp” %>
Welcome to My first Simple Struts Portlet!

2.8 Language-ext.properties

Add a entry,

javax.portlet.title.MYSTRUTSPORTLET= MY STRUTS PORTLET

You have successfully created Struts Portlet.

Create JSP Portlet in Liferay 5.2.X

June 29, 2009 by Gnaniyar Zubair

A Portlet is a  Web component that processes requests and generates dynamic content.

A portlet is a reusable building block for easily publishing information and applications.

Here i am giving the step by step code snippets for creating simple Jsp portlet in Liferay Enviroinment.

To create simple JSP portlet , We need atleast 3 xml files and 1 jsp file.
[the xml files are located under "ext/ext-web/docroot/WEB-INF"]
1. portlet-ext.xml
2. liferay-portlet-ext.xml
3. liferay-display.xml
4. view.jsp

I .  Development

In this xml file, we have to define portlet name and portlet class ..etc…

1.portlet-ext.xml (ext/ext-web/docroot/WEB-INF)

<portlet>

<portlet-name>MYPORTLET</portlet-name>
<display-name>My Portlet</display-name>
<portlet-class>com.liferay.util.bridges.jsp.JSPPortlet</portlet-class>

<init-param>
<name>view-jsp</name>
<value>/html/testing/myportlet/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
</portlet>

2. liferay-portlet-ext.xml (ext/ext-web/docroot/WEB-INF)

We should register that portlet in liferay using this xml file.

<liferay-portlet-app>
//register portlet in liferay
<portlet>
<portlet-name>MYPORTLET</portlet-name>
</portlet>
</liferay-portlet-app>

3. liferay-display.xml (ext/ext-web/docroot/WEB-INF)

this xml file defines Under which category our portlet should be displayed

<display>
// under which category our portlet shoud display in Add Content…
<category name=”category.myportlet”>
<portlet id=”MYPORTLET“></portlet>
</category>

</display>

4. view.jsp

(create this file under “ext/ext-web/docroot/html/testing/myportlet” )

<h2> Welcome to my first Jsp portlet </h2>

II .  Deploy :

Give “ant deploy“  inside  ext or ext-web. Start the server and make sure that your portlet is created.

your portlet title will disply like this: javax.portlet.title.MYPORTLET

go to  ext/ext-impl/src/content/language-ext.properties file

add this entry:

javax.portlet.title.MYPORTLET = MY JSP PORTLET

then again ant deploy and start the server.

Now, You have sucessfully created Jsp Portlet.

jQuery DatePicker

June 26, 2009 by Gnaniyar Zubair

A  jQuery plugin that attaches a popup calendar to your input fields or shows an inline calendar for selecting individual dates or date ranges.

Required Files : Douwnload Zip file of  :

jquery.datepick.js  &  jquery.datepick.css  & images

include these 2 files in Html page:

<script src=”jquery.datepicker.js” type=”text/javascript”></script>
<script src=”jquery.js” type=”text/javascript”></script>

Example: 1

<script type=”text/javascript”>

jQuery(function() {
jQuery(“#datepicker”).datepicker();
});

</script>

<div class=”demo”>

<p>Date: <input id=”datepicker” type=”text”></p>

</div>

Example : 2

Start Date – End Date

<script>

jQuery(function() {

jQuery(‘selector‘).datepick();
jQuery(‘#startDatepicker,#endDatepicker‘).datepick({beforeShow: customRange,
showOn: ‘both’, buttonImageOnly: true, buttonImage: ‘calendar.gif’});
});
function customRange(input) {
return {minDate: (input.id == “endDatepicker” ?
jQuery(“#startDatepicker“).datepick(“getDate“) : null),
maxDate: (input.id == “startDatepicker” ?
jQuery(“#endDatepicker“).datepick(“getDate“) : null)};
}

</script>

<p><span class=”demoLabel“>Date range with separate fields:</span>
<input type=”text” size=”10″  id=”startDatepicker“/> to
<input type=”text” size=”10″  id=”endDatepicker“/></p>

Note:

If you want to get the data when date is clicked instead of button, add below  onSelect Method.

jQuery(‘#datepicker’).datepick({

showOn: ‘both’, buttonImageOnly: true, buttonImage: ‘calendar-green.gif ‘,
onSelect: function()
{
getInvoiceByDate(jQuery(‘#openBillsDate’).val());
} ,

showStatus : true

});

jQuery Loading Indicator

June 26, 2009 by Gnaniyar Zubair

This spinner animation ( loading indicator) is used for loading a small animated indicator  while page  is  loading or any action performed.

busy.js 1.21 allows you to add/remove loading indicators to html elements (or viewport) on your webpages .

Demonstration  (Click to see what cvi_busy_lib.js can do )
Required Files :   Download Zip files of  cvi_busy_lib.js
Example:1

<script type=”text/javascript”  src=”cvi_busy_lib.js”></script>

<script type=”text/javascript” src=”jquery.js”></script>

<button onclick=”block_viewport();”><big>Viewport</big></button>
<script type=”text/javascript” charset=”utf-8″>

var xval, tval, bval, rval, lval;
function block_viewport() {
xval=getBusyOverlay(‘viewport’,{color:’lightgrey’,    opacity:0.75, text:’loading…’, style:’text-shadow: 0 0 3px   black;font-weight:bold;font-size:16px;color:white’},{color:’#ff0′, size:100, type:’o'});
if(xval) {var c=0,t=100;
xval.ntime=window.setInterval(function() {
xval.settext(‘viewport: loading…’+c+’%'); c++;
if(c>t) {window.clearInterval(xval.ntime); xval.remove();}
},50);
}
}

</script>

Example : 2

(Click to add overlay and click again to remove.)

<div id=”top“  onclick=”try {tval.remove(); tval=”;}catch(e) {tval=getBusyOverlay(this,{color:’blue’,opacity:0.25});}“>

<p>Epsum factorial non deposit quid pro quo hic escorol. Olypian quarrels et gorilla congolium sic ad nauseum. Souvlaki ignitus carborundum e pluribus unum. Defacto lingo est igpay atinlay. Marquee selectus non provisio incongruous feline nolo contendre.</p>
</div>

Example : 3

<div id=”left“  onclick=”try {lval.remove(); lval=”;}catch(e) {lval=getBusyOverlay(this,{color:’black’, opacity:0.5, text:’loading’, style:’text-decoration:blink;font-weight:bold;font-size:12px;color:white’},{color:’#fff’, size:128, type:’o'});}“>

<p><img src=”images/spam.jpg” align=”left” alt=”0″ />Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>

<p>Epsum factorial non deposit quid pro quo hic escorol. Olypian quarrels et gorilla congolium sic ad nauseum. Souvlaki ignitus carborundum e pluribus unum. Defacto lingo est igpay atinlay. Marquee selectus non provisio incongruous feline nolo contendre.</p>
<p>Gratuitous octopus niacin, sodium glutimate. Quote meon an estimate et non interruptus stadium. Sic tempus fugit esperanto hiccup estrogen. Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito.</p>
</div>

Example : 4


<div id=”right” onclick=”try {rval.remove(); rval=”;}catch(e) {rval=getBusyOverlay(this,{color:’white’, opacity:0.5},{color:’#00f’, size:32, type:’c', iradius:10, weight:6});}“>

<p>Gratuitous octopus niacin, sodium glutimate. Quote meon an estimate et non interruptus stadium.</p>
<p>Sic tempus fugit esperanto hiccup estrogen. Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito.</p>
</div>

Example : 5


<div id=”bottom” onclick=”try {bval.remove(); bval=”;}catch(e) {bval=getBusyOverlay(this,{color:’green’,opacity:0.25});}“>

<p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
</div>

jQuery Show/Hide

June 26, 2009 by Gnaniyar Zubair

We can show and hide the division using jQuery in different animated view.

It’s a popular smooth “Web 2.0 style” animation effect on many  sites

Example:

<script type=”text/javascript” src=”jquery.js“></script>

<b><div id=”contactLink“></div></b>

<div id=”contactForm“>
This is Main Division which is to be Show / Hide
</div>

<script>

jQuery(“#contactLink”).html(‘Show / Hide Header’);

jQuery(document).ready(function() {

jQuery(“#contactLink”).click(function() {

if(jQuery(“#contactForm”).is(“:hidden”)){

jQuery(“#contactForm”).fadeIn(“slow”);
jQuery(“#contactLink”).html(‘Hide Header’);
}

else {

jQuery(“#contactForm”).fadeOut(“slow”);
jQuery(“#contactLink”).html(‘Show Header’);
}

});

});

</script>

Note:

Instead of fadeIn / fadeOut, we can use these effects:

slideDown / slideUp ==>  e.g.  jQuery(“#contactForm”).slideDown(“slow”);

show/ hide ==>  e.g.  jQuery(“#contactForm”).show(“slow”);

toggle ==>  e.g.  jQuery(“#contactForm”).toggle(“slow”);