Form Submit in Spring MVC Portlet

form-submit-in-spring-mvc-portlet


Today by this article “Form Submit in Spring MVC Portlet” I am going to explain how to submit form in your Spring MVC Portlet. This example is very very basic in Spring MVC. If your basics are strong then you are ready to go for the complex stuffs. By the end of this article you will learn

i) Spring form:form tag

ii) Binding UI data to POJO class

iii) Use of  @ModelAttribute

iv) Retrieve UI data from POJO in the Controller

For the demonstration of Form Submit in Spring MVC portlet a I have created a new portlet. The various navigation is explained as below

1. The first View (view.jsp)

When the portlet is added to the portal page it looks like

spring-form-submission-first-view

Below is the source code of the above page. 

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

<portlet:defineObjects />

<portlet:renderURL var="renderURL">
	<portlet:param name="action" value="showForm"/>
</portlet:renderURL>

<h3> Spring Form Submission Demo</h3>

<br/><br/>

<a href="<%=renderURL.toString()%>">Go to Form Page</a>

On clicking Go to Form Page it will call below method in the controller

	@RenderMapping(params = "action=showForm")
	public String viewByParameter(Map<String, Object> map) {
		log.info("##############Calling viewByParameter###########");

		Customer customer = new Customer();

		/**
		 * Keep the object customer in the map this object will be used in the
		 * JSP as modelAttribute
		 */
		map.put("customer", customer);

		/**
		 * Returning a String "form"
		 *  This means that form.jsp is the view
		 */
		return "form";

	}

Explanation: 

i) A controller can have multiple render method.

ii) The render method is mapped by the parameter action=showForm

iii) Before displaying the form we are creating Object of Customer class and putting in map. This will be used as model attribute in the form.jsp

iv) We can give any name of render method with any parameters. Spring gives this flexibility.

2. The second View (form.jsp)  

After clicking the Go to Form Page link it will display form.jsp. Below is the code 

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:actionURL var="submitFormURL" name="handleCustomer"/>
<form:form name="customer" method="post" modelAttribute="customer" action="<%=submitFormURL.toString() %>">
<br/>
	<table style="margin-left:80px">
		<tbody>
			<tr>
				<td><form:label path="firstName">First Name</form:label></td>
				<td><form:input path="firstName"></form:input></td>
			</tr>
			<tr>
				<td><form:label path="middleName">Middle Name</form:label></td>
				<td><form:input path="middleName"></form:input></td>
			</tr>
			<tr>
				<td><form:label path="lastName">Last Name</form:label></td>
				<td><form:input path="lastName"></form:input></td>
			</tr>
			<tr>
				<td><form:label path="age">Age</form:label></td>
				<td><form:input path="age"></form:input></td>
			</tr>
			<tr>
				<td><form:label path="address">Address</form:label></td>
				<td><form:input path="address"></form:input></td>
			</tr>

			<tr>
				<td colspan="2"><input type="submit" value="Submit Form">
				</td>
			</tr>
		</tbody>
	</table>
</form:form>

Explanation:

use-of-model-attribute

 

i) We are using here <%@taglib uri=”http://www.springframework.org/tags/form” prefix=”form”%>. Its from Spring

ii) The path attribute of form:label is to bind data to the POJO class.


 

Data Binding

spring-form-submission-data-binding


 The source code of the POJO Class Customer.java

package com.proliferay.demo.model;

import java.io.Serializable;

/**
 *
 * @author Hamidul Islam
 *
 */

public class Customer implements Serializable{

	private static final long serialVersionUID = 1L;
	private String firstName;
	private String middleName;
	private String lastName;
	private int age;
	private String address;

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}

3. After Submitting the form
When the form is submitted it call the below method of the controller

@ActionMapping(value = "handleCustomer")
	public void getCustomerData(
			@ModelAttribute("customer") Customer customer,
			ActionRequest actionRequest, ActionResponse actionResponse,
			Model model) {

		log.info("#############Calling getCustomerData##########");

		actionResponse.setRenderParameter("action", "success");

		model.addAttribute("successModel", customer);
	}

After execution of the action method it invokes the below render method

	@RenderMapping(params = "action=success")
	public String viewSuccess() {

		log.info("#############Calling viewSuccess###########");
		/**
		 * Display success.jsp
		 */
		return "success";

	}

Explanation : 
i) In the action method the parameter @ModelAttribute(“customer”) Customer customer retrieve the submitted form data. 

ii) actionResponse.setRenderParameter(“action”, “success”) is used to map render method to be called after execution of the action method. 

iii) model.addAttribute(“successModel”, customer) prepares model data. The object successModel model will be available in the next view i.e, sucess.jsp 

4. Final view success.jsp 

<div>
	<h3>Form submitted successfully</h3>
</div>

<div>First Name: ${successModel.firstName}</div>

<div>Middle Name: ${successModel.middleName}</div>

<div>Last Name: ${successModel.lastName}</div>

<div>Age: ${successModel.age}</div>

<div>Address: ${successModel.address}</div>

5. Full Controller Code CustomerController.java

package com.proliferay.demo.controller;

import java.util.Map;

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.ModelAttribute;
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.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.proliferay.demo.model.Customer;

/**
 *
 * @author Hamidul Islam
 *
 */
@Controller("helloWorldController")
@RequestMapping(value = "VIEW")
public class CustomerController {

	private Log log = LogFactoryUtil.getLog(CustomerController.class.getName());

	/**
	 *
	 * When the portlet is added to the portal page this method will be called.
	 * So this is the default render method
	 */
	@RenderMapping
	public String viewHomePage(RenderRequest request, RenderResponse response) {

		log.info("#################Calling viewHomePage############");

		return "view";

	}

	@RenderMapping(params = "action=showForm")
	public String viewByParameter(Map<String, Object> map) {
		log.info("##############Calling viewByParameter###########");

		Customer customer = new Customer();

		/**
		 * Keep the object customer in the map this object will be used in the
		 * JSP as modelAttribute
		 */
		map.put("customer", customer);

		/**
		 * Returning a String "form"
		 *  This means that form.jsp is the view
		 */
		return "form";

	}

	@RenderMapping(params = "action=success")
	public String viewSuccess() {

		log.info("#############Calling viewSuccess###########");
		/**
		 * Display success.jsp
		 */
		return "success";

	}

	@ActionMapping(value = "handleCustomer")
	public void getCustomerData(
			@ModelAttribute("customer") Customer customer,
			ActionRequest actionRequest, ActionResponse actionResponse,
			Model model) {

		log.info("#############Calling getCustomerData##########");

		actionResponse.setRenderParameter("action", "success");

		model.addAttribute("successModel", customer);
	}

}

 Download Source Code 

 Related Article 

https://proliferay.com/hello-world-spring-mvc-portlet-annotations/

About The Author

4 thoughts on “Form Submit in Spring MVC Portlet”

  1. Pingback: Spring MVC Portlet : form not saving data correctly - BlogoSfera

  2. In order for the model binding in

    @ActionMapping(value = “handleCustomer”)
    public void getCustomerData(
    @ModelAttribute(“customer”) Customer customer,
    ActionRequest actionRequest, ActionResponse actionResponse,
    Model model) {
    to work, you need to add false setting to your liferay-portlet.xml.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d