Developing a JSR 286 Portlet Using Spring MVC
Developing a JSR 286 Portlet Using Spring MVC
A step-by-step approach to develop a JSR 286 portlet with Spring MVC using RAD 8 and deploy in WebSphere Portal 8.5
Join the DZone community and get the full member experience.Join For Free
Spring is a very popular framework, providing lots of features and functionalities organized in a modular fashion. In addition to supporting conventional (servlet-based) web development, Spring also supports JSR-286 Portlet development. The Portlet MVC framework is a mirror image of the Web MVC framework, and also uses the same underlying view abstractions and integration technology.
Just like any other MVC framework , Spring MVC consists of 3 things.
The following is the core architecture of Spring (Web) MVC.
The Front Controller (Dispatcher Portlet) works as the Controller. The front controller takes the incoming request and dispatches to the specific handler (Controller) that has been designed to cater that particular type of request.
The handler (Controller) processes the request and sends the response back to the front controller with the help of Model.
The front controller then determines the particular View with the help of View Resolver and finally sends the response back to the client.
Now let's create a Spring MVC portlet, for example. I will use using Rational Application Developer (RAD) to create the portlet.
Step 1: Open your IDE (Rational Application Developer (RAD) or Eclipse) and create a new portlet project
Step 2: Now modify the type of the portlet and make it a JSR 286 empty portlet
This will create the structure of the web project and will look like this.
Step 3: Now add the spring library and other related JARs in our project. Download the following spring JARs from here. Add the JARs in the /WEB-INF/lib folder of the project.
Step 4: The next step is to add the entry of "View Renderer Servlet" in web deployment descriptor. This step is very important. A Spring portlet is an extension of the Spring Web MVC, and Spring provides support of all its functionalities in the portlet context with the help of this renderer servlet. This servlet works as a bridge between Spring Web MVC and Portlet MVC.
Also, add the following entry of the Context Loader Listener. This will tie the Application Context to the life cycle of the Servlet Context and automate the creation of the Application Context.
Step 5: Open the portlet deployment descriptor (portlet.xml) and list the dispatcher portlet. As described above, the dispatcher portlet will serve as the front controller and will be responsible for dispatching the request to the controller specified to handle that particular type of request.
There is another entry that you can provide here in portlet.xml — "contextConfigLocation". That's in case you want to have a custom name or location for the application context file. If you do not provide any specific location, Spring will look for the Application Context file inside "/WEB-INF" by default.
Step 6: Create a file named "ApplicationContext" inside "/WEB-INF" and add the entry of "ViewResolver" there. In Spring, "DispatcherPortlet" will take the help of "ViewResolver" to choose the view (JSP in our case). So we need to configure the view (through View Resolver) in the Spring application context file. Add the following entry in the context file
Step 7: Now we will create another context file and place it inside WEB-INF folder. The naming convention of the context needs to be <<portletName>> - portlet.xml. So, in our case, it will be "MyFirstSpringMVCPortlet-portlet.xml". This is a context file we will maintain to place all portlet-specific configuration parameters.
Inside the config file, register your controllers and DefaultAnnotationHandlerMapping. The DefaultAnnotationHandlerMapping maps requests to classes and/or methods that are annotated with @RequestMapping.
You may find it confusing to maintain two different config files (Application Context and MyFirstSpringMVCPortlet-portlet.xml). It is not mandatory, but I am following a good practice by separating the application-specific configuration with portlet-specific configuration.
Step 8: Now we need to create the controller class in the same package as registered in the context file.
@Controller is used to denote that this is our controller. The value will be same as class name. The second annotation is @RequestMapping(“VIEW”), which tells the DispatcherPortlet that this controller will support VIEW mode. Similarly, a separate controller has to be added for other modes (EDIT, HELP, etc.).
The @RenderMapping annotation denotes the default render method. It means whenever we place this portlet, it will render this method.
To handle render requests, action requests, or event requests, corresponding methods need to be added with the following annotations, respectively:
In our example, the method returns a ModeAndView object. The ModelAndView object tries to resolve the view name (view.jsp) in the path we specified in the our view resolver entry in the previous step. We have specified the prefix as "/WEB-INF/jsp/" and the suffix as ".jsp". So, the DispatcherPortlet, with the help of ViewResolver, will try to find "/WEB-INF/jsp/view.jsp" and will try to render it.
Step 9: Finally, we need to create the JSP in the same location so that it can be rendered.
We will write a bit of text in the JSP to display after a successful rendering of the portlet.
Step 10: Now we are all set to deploy and test our Spring MVC portlet. Once deployed and added to a portal page, it will render and display the JSP.
Published at DZone with permission of Soumyajit Sarkar . See the original article here.
Opinions expressed by DZone contributors are their own.