Sunday, 24 September 2017

Form Submission in Spring MVC 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
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:actionURL name="login" var="login">
<portlet:param name="action" value="loginSubmit"></portlet:param>
</portlet:actionURL>
<form action="${login}" method="POST">
Name: <input type="text" name = "username"><br>
Password:<input type="password" name="password"><br>
<input type="submit" value="SUBMIT">
</form>
view rawlogin.jsp hosted with ❤ by GitHub


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
<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">
<liferay-portlet-app>
<portlet>
<portlet-name>login</portlet-name>
<icon>/icon.png</icon>
<requires-namespaced-parameters>false</requires-namespaced-parameters>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
<css-class-wrapper>login-portlet</css-class-wrapper>
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
<role-name>guest</role-name>
<role-link>Guest</role-link>
</role-mapper>
<role-mapper>
<role-name>power-user</role-name>
<role-link>Power User</role-link>
</role-mapper>
<role-mapper>
<role-name>user</role-name>
<role-link>User</role-link>
</role-mapper>
</liferay-portlet-app>
view rawliferay-portlet.xml hosted with ❤ by GitHub

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
package com.login;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import com.liferay.portal.kernel.util.ParamUtil;
@Controller(value = "Login")
@RequestMapping("VIEW")
public class Login {
//Default Render Method
@RenderMapping
public String handleRenderRequest(RenderRequest request,RenderResponse response, Model model) {
return "login";
}
//After action This method decide the flow of jsp
@RenderMapping(params = "action=renderAfterAction")
public String afterLoginRenderMethod(RenderRequest request,RenderResponse response) {
String jspValue = (String) request.getAttribute("jspValue");
if (jspValue.equalsIgnoreCase("fail")) {
return "login";
} else {
return "success";
}
}
@ActionMapping(params = "action=loginSubmit")
public void loginSubmit(ActionRequest request, ActionResponse response) {
String userName = ParamUtil.getString(request, "username", "");
String password = ParamUtil.getString(request, "password", "");
if (userName.equalsIgnoreCase(password)) {
response.setRenderParameter("action", "renderAfterAction"); //This parameter decide which method is called after completion of this method
request.setAttribute("jspValue", "success"); //Attribute use for which jsp is render
request.setAttribute("userName", userName); //Attribute fetch on success.jsp
} else {
response.setRenderParameter("action", "renderAfterAction");
request.setAttribute("jspValue", "fail");
}
}
}
view rawLogin.java hosted with ❤ by GitHub


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")                      ThisrenderAfterAction 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
<h1>successfully login</h1> <br>
<h2>Name = =>${userName}</h2>
view rawsuccess.jsp hosted with ❤ by GitHub

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

TEST

I am Java Developer. I have 6 year Experiance in this field and like to post in blogging. So keep sharing and like my post

0 comments:

Post a Comment

 

Copyright @ 2017 Liferay Article.