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
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:
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:
With this configuration we have to create the messages.properties 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:
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 :
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:
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.
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.