Wednesday, April 13, 2011

Struts Tips

Hi Friends ,
I was working on one of my j2ee project . From there I want to share few small but usefull tips for struts.In struts many time we caught in wrong side for duplicate page submission. I am going to share how to tackle that
and how to pass parameter once redirect is done, one way is there to put parameters in session but it is messy.
Manage Redirect:
It is quite simple we need to add one attribute redirect="true" in struts-config.xml
e.g.
<action path="{Action-Path}" type="{Action}" name="{Form-Bean}">

<forward name="{Forward-Name}" path="{Forward-Path}" redirect="true"/>
</action>
Pass Parameter After Redirect:
In action file in place of using mapping.findForward("{Action-Path}") use ActionRedirect (subclass of ActionForward).This class includes an addParameter() method.
e.g.
ActionRedirect ar = new ActionRedirect(mapping.findForward("{Action-Path}"));
ar.addParameter("{Parameter-Name}", {Parameter-Value});
return ar;
Or
return (new ActionRedirect(mapping.findForward("{Action-Path}"))).addParameter("{Parameter-Name}", "{Parameter-Value}");
Here all words with in {} are place holders.

Thursday, March 17, 2011

I18N Liferay 6.x

Hi today I will share how to use in built Liferay I18N functionality to your custom portlet in Liferay 6.x

It is very simple to add it , for that we need to modify just 2 files.

1.portlet.xml --- In this file add resource bundle entry.

e.g <resource-bundle>content.Language</resource-bundle>

2.build.xml --- In this file we need to call below target.

<target name="build-lang">

<antcall target="build-lang-cmd">

<param name="lang.dir" value="docroot/WEB-INF/src/content" />

<param name="lang.file" value="Language" />

</antcall>

</target>

Now we need to just add one file with name Language.properties under docroot/WEB-INF/src/content
from build.xml using ant call buil-lang. That it !!!

Wednesday, March 16, 2011

CK editor in Liferay 6.0

Hi Friends,
In this article I just want to share how to use CK editor in liferay 6.0 .

Follow these 4 simple steps.

Step 1.Add below line on your page
<liferay-ui:input-editor width="80%" />.


Step 2.Create hidden variable to set the value of CK editor like below
<aui:input name="content" type="hidden" />


Step 3.Add javascript init method like this

<aui:script>

function <portlet:namespace />initEditor() {

return "<%= UnicodeFormatter.toString(content) %>";

}

</aui:script>


Step 4:On submiting form we can assign CK editor value like this

function <portlet:namespace />saveEntry() {

var message = window.<portlet:namespace />editor.getHTML();

document.<portlet:namespace />fm.<portlet:namespace />content.value = message;

submitForm(document.<portlet:namespace />fm);

}


Currently we are having problem in liferay 6.0 is that when we refresh CK editor page , it gives JS error.

For that fix is like this get html/js/editor/ckeditor.jsp in function initCkArea() comment line 52 and 53 which starts with CKEDITOR.config.toolbar and CKEDITOR.config.customConfig respectively and add below line

ckEditor.setData(parent.<%= HtmlUtil.escape(initMethod) %>());
In the same file comment line number 131 i.e

initCkArea(); and add
if (parent.AUI) {

parent.AUI().on('domready', initCkArea);

}
In side CKEDITOR.replace method at line number 123 in same file comment line starts with filebrowserBrowseUrl , filebrowserUploadUrl and
add below code

customConfig: '<%= PortalUtil.getPathContext() %>/html/js/editor/ckeditor/ckconfig.jsp?p_l_id=<%= plid %>&p_p_id=<%= HttpUtil.encodeURL(portletId) %>&p_main_path=<%= HttpUtil.encodeURL(mainPath) %>&doAsUserId=<%= HttpUtil.encodeURL(doAsUserId) %>&cssPath=<%= HttpUtil.encodeURL(cssPath) %>&cssClasses=<%= HttpUtil.encodeURL(cssClasses) %>',
filebrowserBrowseUrl: '<%= PortalUtil.getPathContext() %>/html/js/editor/ckeditor/editor/filemanager/browser/liferay/browser.html?Connector=<%= connectorURL %>',
filebrowserUploadUrl: null,
toolbar: '<%= TextFormatter.format(HtmlUtil.escape(toolbarSet), TextFormatter.M) %>'


Finally add required import i.e
<%@ page import="com.liferay.portal.util.PortalUtil" %> and one extra variable


String portletId = ParamUtil.getString(request, "p_p_id");

Wednesday, February 23, 2011

Simple java program to read json from text file

Hi All,
was busy some what from last couple of months. Finally started on liferay 6.0 from last couple of week.Today want to share simple program which can be used to read a text file and read json from it using apache commons library. Using apache commons library we can reduce our code to large extent. Here is the program.

import java.io.IOException;
import java.io.InputStream;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.io.IOUtils;


public class ClassloaderTest {

public static void main(String [] args) {
ClassLoader cl = ClassloaderTest.class.getClassLoader();
InputStream is = cl.getResourceAsStream("json.txt");
try {

String s = IOUtils.toString( is );
JSONObject json = (JSONObject) JSONSerializer.toJSON(s);

JSONObject searchresults = json.getJSONObject("searchresults");
System.out.println("ssssssss" + searchresults.optString("count", ""));
JSONArray jarray = searchresults.getJSONArray("URI");
for(int i=0 ; i < jarray.size(); i++) {
System.out.println("jarray [" + i + "] --------" + jarray.getString(i));
}
} catch(IOException e) {
e.printStackTrace();
}
}



}

and content of json.txt is below
{
"searchresults": {
"URI": ["qa1", "qa2", "qa3", "qa4", "qa5"],
"count": "5"
}
}

jason.txt you can keep in the same package where your class is.
Hope this post helps others.
Kamal