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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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> |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>successfully login</h1> <br> | |
<h2>Name = =>${userName}</h2> |
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
Oh! Wonderful blog. I had no knowledge about Spring MVC Portlet. After reading this blog, I know many things about this. Here everything writes point wise, so that anyone can easily understand and implement also. I must use this. I know about myasp.net which is a hosting site. It provides different types of hosting. Thanks to share.
ReplyDelete