Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CRUD with Spring MVC Portlet: Creating the project and the high action

1. Environment

This tutorial is written using the following environment:

  • Hardware: Mac Book Pro 17 “Notebook (2.6 Ghz Intel Core
    i7, 8 GB
  • Operating System: Mac OS X Snow Leopard 10.6.4
  • Spring MVC Portlet 3.0.4
  • Maven 2.2.1
  • Eclipse 3.6 (Helios) with M2Eclipse
  • Liferay 6.0.5

2. Introduction

This time we will “enrich” the example by making a Portlet that allows us to perform all CRUD operations against an entity. To perform the operations against the database, I will support the project that we created in this tutorial.

3. Creating the project

The only thing that will change (if you want) is the name of the artifactId, which we will call spring-mvc-crud-portlet.

4. Integrating the project with the model

Data Access Library with Spring and JPA we saw how to create the data model that we are now going to integrate with our project.

To integrate the model we have to add the dependency in the pom.xml of our project, in this way:


Now to be able to use this dependency in our project we have to edit the file WEB-INF / context / applicationContext.xml of our project to add the following line:

What we are doing is incorporating the Spring configuration of our model into our project, so we will only have to declare the DAOs to be able to use them in our classes.

5. Creating the detail view of the person

Now we are going to create the file detail_person.jsp inside the folder jsp / spring-crud-mvc-portlet with the following content:

@ taglib uri=“” prefix=“portlet”%>  
@ taglib uri=“” prefix=“spring”%>  
@ taglib uri=“” prefix=“form”%>  
portlet:defineObjects />  
portlet:actionurl var=“executeActionURL” name=“executeAction”>  
    portlet:param name=“action” value=“${futureAction}”>  
form:form action=“${executeActionURL}” commandname=“personForm”>  
    form:hidden path=“idPerson”/>  
        spring:message code=“” />  
        form:input path=“name” />  
        spring:message code=“person.surnames” />  
        form:input path=“surnames” />  
        spring:message code=“person.address” />  
        form:input path=“address” />  
    spring:message code=“${futureAction}” var=“formSubmit”/>  
    spring:message code=“form.cancel” var=“formCancel”/>  
    input type=“submit” value=“${formSubmit}”/>  
    input type=“submit” value=“${formCancel}” name=“form.cancel”/>  

The only thing we are doing is creating a form that will help us to see the detail of a person and be able to make a registration, a modification or eliminate it. For now we are going to focus on the high of the person.

Note that to create the form we are using spring tags, form refers to the components of the form and spring: message allows us to internationalize the messages.

To register the message file we have to add the following to the applicationContext.xml file of our project:

bean id=“messageSource” class=“”>  
        property name=“useCodeAsDefaultMessage” value=“true” />  
        property name=“basenames”>  

With this configuration we have to create the file inside the src / main / resources folder where we will register all the keys that we use.

By declaring the property “useCodeAsDefaultMessage” to “true” we get that if Spring does not find the key of the message paint the text of the key, instead of giving a deployment error.

We have also declared in the commandName attribute of the form that we are going to use a bean called personaForm that represents the fields of the form and allows Spring to map the fields of the form, it will have the following content:

package com.autentia.springcrudmvcportlet.forms;
import com.autentia.tutoriales.model.Person;
public class PersonForm implements Serializable{
private static final long serialVersionUID = 8425267500435219790L;
private Long idPerson;
private String name;
private String surnames;
private String address;
public PersonForm(){}
public PersonForm(Person person){
this.idPerson = person.getIdPerson(); = person.getName();
this.surname = person.getSurname();
this.address = person.getAddress();
public Long getIdPerson() {
return idPerson;
public void setIdPerson(Long idPerson) {
this.idPerson = idPerson;
public String getName() {
return name;
public void setName(String name) { = name;
public String getSurnames() {
return surnames;
public void setSurnames(String surnames) {
this.surnames =surnames;
public String getAddress() {
return address;
public void setAddress(String address) {
this.address = address
public Person getPerson(){
final Person person = new Person();
return person;

6. Register people

For the registration of people the first thing we are going to do is create the file lista_personas.jsp that will be the main view of our portlet and from where we will call the action of adding a person, the content of this file is the following :

@ taglib uri=“” prefix=“portlet”%>  
@ taglib uri=“” prefix=“spring”%>  
@ taglib uri=“” prefix=“form”%>  
@ taglib prefix=“c” uri=“” %>  
portlet:defineObjects />  
c:if test=“${msgstatus != null}”>  
p class=“portlet-msg-success”>${msgstatus}/p>  
portlet:renderurl var=“detallePersonAltaURL”>  
    portlet:param name=“futureAction” value=“crud.alta”>/portlet:param>  
p>spring:message code=“form.anadirPersona”/>/p>

With the portlet: renderURL tag we are creating the necessary URL to be able to navigate to the detail window indicating that the action we want to perform is the registration.

To visualize this jsp as the default screen of our portlet, all we have to do is indicate it in the function marked with @RequestMapping in this way:

protected final String defaultView(Model model) {
return “people_list”;

In this way the first thing we will see when loading the portlet will be a link to the screen that allows us to register a person, which is none other than detalle_persona.jsp

To redirect our portlet to this screen, we must first capture the render action in this way:

With the annotation @RequestMapping (params = “futuraAccion”) what we do is declare that this method of rendering will be executed when the URL is passing a parameter called “futuroAccion” regardless of its value.

With the annotation @RequestParam (“futuroAccion”) we are recovering the concrete value of this parameter, which we are going to introduce in the model since it will serve us to determine which action has to execute the action button of the detail form of the person.

Now we have to create the method that is going to perform the persistence of the person, which will be invoked when there is the parameter “executeAction” inside the URL and the parameter “action” matches the text “crud.alta”. With what the method would be declared in this way:

In case the user clicks on the cancel button, we do not do anything, and if you click on the high button, we directly enter that information into the database, using our model-tutorials project.

In a “real” case, it is probably not interesting to directly access the database from the controller and a service layer will have to be created to make this access, in order to reuse the code or create a REST layer, for example. The design will depend on the scope of our project.

As we do not say otherwise, after executing this method the portlet will show the default view, showing a message process performed correctly.

7. Let’s try it

To test our development, we only need to run mvn clean package of our project, and deploy our .war on the Liferay server. In case of using the Liferay plugin, we can do it directly by executing: mvn liferay: deploy, this action does NOT previously make the project package.

Once deployed correctly, we instantiate the portlet on any page of our portal and we should see something similar to this.

To access the registration screen, click on the link that appears, fill in the form data and click on the registration button, so if we go to the configured database we can see that our registration has been inserted.

8. Conclusions

We are going to leave it here, in the next tutorial we will see how to show a list with all the people we are registering, using the displaytags library that allows us to have tables in a very comfortable way. In addition, in later tutorials, we will see how to modify and eliminate people, such as adding validation to our form and doing unit tests.

The post CRUD with Spring MVC Portlet: Creating the project and the high action appeared first on Target Veb.

This post first appeared on Targetveb, please read the originial post: here

Share the post

CRUD with Spring MVC Portlet: Creating the project and the high action


Subscribe to Targetveb

Get updates delivered right to your inbox!

Thank you for your subscription