Showing posts with label Spring Portlet. Show all posts
Showing posts with label Spring Portlet. Show all posts

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

Spring MVC Portlet in liferay

Spring MVC Portlet in Liferay 


Today we will discuss how to create Spring Portlet in Liferay.For doing this first we create a simple Portlet and then convert it into Spring Portlet.By using Spring Portlet we can handle multiple action request ,multiple resource request in seperate methods.We can have multiple action methods in single class.
Lets Start this step by step:-


Step 1:-Create liferay Plugin Project
Open eclipse click on :-
file->New->Liferay Plugin Project.
Give project name as FirstSpringMVC and finish.







Step 2:-Create MVC Portlet in project
Right Click on project->New->Liferay Portlet



This will open a new window provide portlet class name, package name and select superclass and Click Finish.



This will create a basic portlet .Now we have to convert it into Spring portlet.

Step 3:-Provide Spring jars to classpath
For using Spring we have to provide Spring dependency jars to class path.Following are the list of jars:-

  • spring-web-servlet.jar
  • spring-web-portlet.jar
  • spring-web.jar
  • spring-transaction.jar
  • spring-jdbc.jar
  • spring-expression.jar
  • spring-core.jar
  • spring-context.jar
  • spring-beans.jar
  • spring-asm.jar
  • spring-aop.jar
  • spring-aspects.jar
  • spring-context-support.jar
  • spring-jms.jar
  • spring-orm.jar
  • spring-oxm.jar
  • spring-web-struts.jar
  • commons-beanutils.jar
  • commons-collections.jar
  • commons-fileupload.jar
  • commons-io.jar
  • commons-lang.jar
  • jstl-api.jar
  • jstl-impl.jar 
So open liferay-plugin-package.properties from web-inf and add the following jars.

liferay-plugin-package.properties


Step 4:-Provide ViewRendererServlet in web.xml
ViewRendererServlet is a bridge servlet, mainly for the Portlet MVC support.For usage with Portlets, this Servlet is necessary to force the portlet container to convert the PortletRequest to a ServletRequest, which it has to do when including a resource via the PortletRequestDispatcher. This allows for reuse of the entire Servlet-based View support even in a Portlet environment.

web.xml

Note=>These are hard code entries and same for every project

Step 5:-Provide entry of Front Controller
Front Controller handle all request/response flow it is mention in portlet.xml as:-

<portlet-class> 
org.springframework.web.portlet.DispatcherPortlet
</portlet-class>

Step 6:-Create Spring Application Context File
Front Controller after receiving request take help from some helper classes these classes are mention in a seperate file whose name is :-
<portlet-name>-portlet.xml 
Ex- first-controller-portlet.xml.

Any Spring Project contain atleast one application context file where all beans and helper classes are defined.So create a folder inside WEB-INF named spring-portlet-config and inside this folder create first-controller-portlet.xml.

first-controller-portlet.xml


Explanation:-


1)DefaultAnnotationHandlerMapping
The helper that help Front Controller that which controller is handled which request ie help to find the controller because there are many Controller but only one Front Controller.

2)<bean class="com.first.FirstController" />
Here we provide the full path of our controller class if we have more than one controller than we can use tag 
<context:component-scan base-package="com.first" />
and provide the package path where all controller resides.

3)<bean id="jspViewResolver" class = " ">
When controller return the name of jsp front controller take help from view Resolver to find the exact path of jsp by using prefix and suffix properties.

4)<context:annotation-config /> 
Because we are using annotation to register bean this tag is mandatory if we are using annotation.

Step 7:-Provide entry of ApplicationContext file
We have to provide the entry of this context file in portlet.xml so that our Front Controller find this file to delegate the request to this file.

Now our portlet.xml look like :-

portlet.xml
Step 8:-Create Controller Class
Open class FirstController and paste this :-

FirstController.java
Explanation:-


1)@Controller(value = "FirstController")
This annotation is class level annotation and register this class as a controller the value is name of class.

2)@RequestMapping("VIEW")
This annotation tell that this controller handle view mode for edit mode you have to create a new controller.

3)@RenderMapping
This annotation is method level annotation and tell that this method is treated as render method.

4)return "hello"
Name of the jsp file which is to be render.

Step 9:-Create View
Here our view is jsp so create hello.jsp (/docroot/html/firstcontroller/hello.jsp)

hello.jsp

Step 8:-Deploy Your Project
Right Click on build.xml->Run As->Ant Build.

Thats it final project Structure is:-



You can download source code from Spring MVC Portlet in Liferay





Hope this will help....

 Related Post:-

Form Handling in Spring MVC Portlet

Ajax in Spring Portlet

Custom Sql in Liferay

Many To Many Relationship mapping in Liferay Services

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven