Portlet Name Space Parameter in Liferay

Use Of Name Space Parameter in Liferay


Today we will discuss the use of Portlet Name Space parameter in Liferay.If you are using Liferay 6.1 then you can get the value of form input values easily but for Liferay 6.2 it is compulsory to use the namespaces.


Q) What is Portlet Name Space?
Ans:As you already know you can put many portlet on the same page in liferay.Let if there are 2 portlet on the same page and both have a input box with a same name like username what will happen answer is Name coflict. To avoid this name conflict we use portlet name space.

Portlet Name Space will give unique name to the elements in the page and these elements are associated to respective portlet. This will avoid name conflicts in the page.


How To Deal with Name Space?





Their are basically 3 ways:-

1)Use basic tags and provide entry in xml

 <form action="${namespace}" method="Post">
  Name :<input type="text" name="name" id="name"> 
<input type="Submit" name="Submit" value="Submit">
</form> 

and put requires-namespaced-parameters is false inside liferay-portlet.xml.

<requires-namespaced-parameters>
    false
</requires-namespaced-parameters>

2)Use <portlet:namespace/> tag with fields

<form action="${namespace}" method="Post">
Name :<input type="text" name="<portlet:namespace/>name" id="<portlet:namespace/>name">
<input type="Submit" name="Submit" value="Submit">
</form>

For using this you have to import taglib as:-

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

3)Use aui tags

<aui:form action="${namespace}" method="Post">
<aui:input name="name" id="name" />
<aui:button value="Submit" type="Submit"></aui:button>

</aui:form>

For using this you have to import taglib as:-

<%@taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>

Note:- Aui tags automatically append name spaces








Hope this will help....

 Related Post:-

Spring MVC portlet in Liferay

Form Handling in Spring Portlet

Many To Many Relationship mapping in Liferay Services

How to Use Ajax in Spring Portlet

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven








Ajax Call in Spring MVC Portlet

How To Use Ajax  in Spring Portlet


Today we will discuss how to use Ajax in Spring Portlet. For this we create two select box one for country and second for States. When someone select country Ajax call is generated and dynamically add options in States select box depending on which country is selected.Before reading this blog it is highly recommended to read my previous blog Spring Portlet in Liferay and Form Handling in Spring Portlet .
Lets Start this step by step:-



Step 1:-Create Spring MVC Portlet
For creating a basic Spring MVC portlet you can refer my previous blog Spring MVC Portlet in liferay.

Step 2:-Attach jquery in Portlet
For using jquery open liferay-portlet.xml and in header javascript attach jquery.min.js

Ex-
<header-portlet-javascript>
   http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js
</header-portlet-javascript>

Also put requires-namespaced-parameters is false inside liferay-portlet.xml.

Ex-
<requires-namespaced-parameters>
    false
</requires-namespaced-parameters>






Step 3:-Create Ajax in your view.jsp
Open view.jsp and paste this content:-

view.jsp

Explanation:-

1)In line 4 we create a resource Url findState ,this URL is used to create a method in our controller that is using this value as:-
@ResourceMapping(value="findState")

2)In Line 6 to 27 we create our Ajax call that can use URL findState and send the name of country in a parameter countryName. If data return successfully in the form of json we parse the json data and by using for loop append the data in state select box.

Step 3:-Handle the Ajax Call
Open your controller and create a method that is call when Ajax is generated.:-

AjaxController.java

Explanation:-
1)Here we create a method findStateForCountry that is annotated with @ResourceMapping(value="findState") this find state must match with the id of resourceUrl that is created in view.jsp.

2)In method findStateForCountry we create a Json Array and two Json objects and put value in the json object on the basis of India or USA and return the response.In json object we add two property one is id of state and second is name of state.This array is iterated in view.jsp to create state drop down.


Output:-




Project Structure:-



You can download source code from Ajax in Spring  Portlet 

Hope this will help....




 Related Post:-

Spring MVC portlet in Liferay

Form Handling in Spring Portlet

Many To Many Relationship mapping in Liferay Services

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven








Form Submission in Spring MVC Portlet

Form Submission in Spring Portlet


Today we will discuss how to create a simple Login form by using Spring Portlet.In the form we create two input box one for Name second for Password and a Submit button.When user enter same value in name and password it goes to success page otherwise return to login page.Before reading this blog it is highly recommended to read my previous blog Spring Portlet in Liferay
Lets Start this step by step:-



Step 1:-Create Spring MVC Portlet
For creating a basic Spring MVC portlet you can refer my previous blog Spring MVC Portlet in liferay


Step 2:-Create a Simple Form
Just create a jsp file login.jsp and paste this content.

login.jsp

Explanation:-
In Line 5 Here we create a actionUrl the value of param ie loginSubmit must match with the @ActionMapping param value inside Controller.







Step 3:-Change Required Namespaces
Here we are not using any namespaces and not using AUI form so we must disable namespace parameter otherwise values of name and password not available in request because it is compulsory in liferay 6.2. So open liferay-portlet.xml and after icon tag paste this:-

liferay-display.xml

Note:-

If you are using AUI form or namespaces than you can skip Step 3.

Step 4:-Create method in your Controller
Open your controller class and paste this content:-

Login.java


Explanation:-

1) handleRenderRequest method

This method is called when you deploy your project return type of this method is String so we return login because our jsp name is login.jsp.

2) loginSubmit method
This method is called when we submit our login form 

  • coz @ActionMapping(params = "action=loginSubmit")   matches with actionUrl params which is created in login.jsp:-<portlet:param name="action" value="loginSubmit">
  • response.setRenderParameter("action", "renderAfterAction"); Here we set renderParameter this action must match with the method  that is annotated with @RenderMapping.So that after completion of this method it will go to proper render method
  • request.setAttribute("jspValue", "success"); This jspValue attribute is used to decide which jsp is call this is available in our renderAfterAction method.
  • request.setAttribute("userName", userName);                       This userName attribute is used to display name on success.jsp.

3)afterLoginRenderMethod method
This method is annotated with:-
@RenderMapping(params = "action=renderAfterAction")                      This renderAfterAction must match with the parameter that is set in our action method as:-
response.setRenderParameter("action", "renderAfterAction"); 
In this method we just fetch the value of jspValue that is set in action method and if value is fail than return login ie goes to login.jsp otherwise return success ie goes to success.jsp.

Step 5:-Create success.jsp
Just paste this content in success.jsp:- 

success.jsp

here we just fetch the attribute userName that is set in our loginSubmit method.

Output:-


Project Structure:-



You can download source code from Form Handling in Spring  Portlet 





Hope this will help....

 Related Post:-

Spring MVC portlet in Liferay

Ajax in Spring Portlet

Many To Many Relationship mapping in Liferay Services