Menu

Liferay 6.2: Using SessionClicks utility


I am sure that you have to use temporary data in building the website, portal. For examples: register the items which are selected in commercial website or memory the status of menus.
There are many methods which help to save that temporary data. In this post, I am going to talk about using SessionClicks utility in JSP and Java code and Liferay.Store in Javascript.

Let's see our example:
Problem: We have a simple problem: Firstly, we will create a div tag, this tag is going to hide or show depend on the current status. Next , we will create three link tags (a tag), their names are RefreshShow, and Hide. Their functions are reloading the page, showing and hiding above div tag. The hiding status of div tag will store in temporary and it will not change after reloading the current page. The following picture contains four components a div tag and three link tags.
Our objective is the picture will be hidden when someone clicks on Hide link. Now, you click on Refresh link or F5, the picture still keeps the previous status (it still be hide). You can get the same result when someone clicks on  Show link.
I have two solutions: i) using only JSP code and ii) using Javascript code. Let's begin with the first method.

JSP Code

Let's see the following snippet and I am going to explain the meaning  of each line:

We will consider it line by line:
  • We ignore the first two lines and you can get the detail of init.jsp file on GitHub (link at the last of this post).
  • Line 5th: We will get the parameter when you click on Show or Hide link (these parameters were added at line 29th and 36th). If you click on other buttons, the value of this parameter is null.
  • Line 7th: Check whether the value of the parameter is not equal null (it means that you click on Show or Hide button). Now we will save current status (line 9th).
  • Line 9th: We will save the temporary value with status key (by using SessionClicks.put method).
  • Line 12th: Reading value the temporary parameter by using SessionClicks.get method.
  • Line 16th: Depend on the value of the status variable we will set up the value of div's class attribute is hide or blank.
Currently, we knew how to use put and get method in SessionClicks to save and get the temporary variables. However, we have to refresh our portlet once we click on Show or Hide link. Therefore, the next approach will allow does not reload the page when you click on Show or Hide link.

Javascript + JSP Code

Let's see the following snippet and compare with the above method:

The components do not change, we only change some lines of code. We are going to consider that changes:
  • In the first snippet, we only keep the expression which gets the value of the temporary parameter. We ignore another component because this temporary value will save when we call the setStatus function in Javascript.
  • setStatus function is called when you click on Show or Hide link. The parameter of this function will be used in showing or hiding the div tag and storing (see line 42nd).
  • Clearly, we do not reload the current page when we click on Show or Hide link but we still get the  desired results.
  • Now let's click on Refresh link, the status of div tag is stored, right?

Conclusions

I introduced the two simple instances about using SessionClicks utility to save the temporary data. My example only solves a simple problem but depends on your issue let's select the best solution.
Click here to download the source code and thanks for visiting.

Không có nhận xét nào:

Đăng nhận xét