Thursday, January 16, 2014

Liferay: Custom 404 Page for plugins

Liferay portal runs and is configured as ROOT application with in tomcat sever.
By default, if tried to request a plugin in Liferay (say we have a theme plugin named as "error-theme"), a 404 Error is raised and the user is served Tomcat's default 404 error page as below:


To make your 404 page a bit more presentable, provide the user with a link back into your site, as well as hide the Tomcat version in use, you can easily add a custom 404 error page.

Step 1:

Create a jsp name it 404.jsp, with below content:


The requested resource is not available. Please provide the correct URL or try again later.
<br/>
<a href="#" onclick="javascript:history.go(-1);">Go back to main site.</a>

Step 2:

Now create a new directory as error-theme/docroot/errors in Lifeay IDE and copy the above created 404.jsp to it.

Step 3:

Last step is to add the below code to the web deployment descriptor (web.xml) of the plugin:

         <error-page>
  <error-code>404</error-code>
  <location>/errors/404.jsp</location>
 </error-page>

Now, when someone goes to the plugin link that does not exist, they get the custom 404 page we created.





Tuesday, January 14, 2014

JQuery : Declare a function



Method 1: The Javascript way

The simple way to declare a  function in javascript:


function callMe (count) {
     return (count+5);
}

console.log(callMe(13));
//output: 18

Disadvantage: If you wanted a quick function to test something then maybe that’s the only occasion you would use this. It’s not good coding and doesn’t promote code reuse.


Method 2: Creating your JQuery function

The simple way to declare a  function in javascript:


<div id="mydiv">i am here</div>

jQuery.fn.extend({
    mytext: function () {
        var text = $(this).text();
        var mytexts = '';
        var toggle = true; //lower/uppper toggle
            $.each(text, function(i, nome) {
                mytexts += (toggle) ? nome.toUpperCase() : nome.toLowerCase();
                toggle = (toggle) ? false : true;
            });
    return mytexts;
    }
});

console.log($('#mydiv').zigzag());
//output: #1 i aM HeRe

//chained example
console.log($('#mydiv').zigzag().toLowerCase());
//output: #1 i am here


Method 1: Functions in custom namespaces

If your writing functions in a custom namespace you must declare them in this way. Extra functions can be added to the namespace you just need to add a comma after each one (except the last one!).


JQUERY4U = {
    callme: function(count) {
        return (count + 5);
    }
}
//function call
JQUERY4U.callme(13);

Reference post: Full credit goes to the original post, which made the topic so easy to understand (visit for more details).