Apache Pluto Portlet & Struts 2 Integration Example Tutorial

In earlier tutorials, we’ve developed different types of Portlet. Portlets are developed using of JSP & Servlet at once and then we learned using of JSF with Portlets. This time we’re going to develop a Portlet using another famous framework like Apache Struts.

Srtuts framework is an MVC-based one, we’ve already covered a lot of topics onto Apache Struts as you can refer for all of them, but this is the first time you will get a Struts application deployed into a Portlet container like Apache Pluto.

You’re likely going to use Apache Struts Bridge that is already maintained by Apache itself. However, the same application that you’ve implemented before (Employee Registration Form) will be used in this tutorial but with Struts2 framework.

The main goal of this application is to help you know all the details required for getting the application deployed and running properly onto any JSR-168/JSR-286 compliant Portlet container.

Final Project Structure

Struts-Project-Final-Project-Structure

Employee Table Design

This section would clarify you what the Employee Table is and what are these columns used.

Employee.sql

Let’s see the Employee entity (Model) that would be used for holding the data that are going back and forth between the application and the database store.

Create Template Application Using Eclipse IDE

You may save your time instead of getting delve into creating the project by yourself, by using an Apache Struts Archetype. Following steps will help you creating a Struts Portlet application easily.

  • Add Remote Archetype Catalog https://struts.apache.org/ into your Maven Archetypes.
  • Create a new Maven project. This time you will get stopped into Catalog selection, instead of using All Catalogs, you must select that’s one registered.
  • Fill in the all required information, GroupId, ArtifactId, version, and Package.
  • Click Finish and waiting your Eclipse displaying created application into your Project Explorer.
  • Though the application is created successfully, but it’s likely want some of in-hand adjustment as the Archetype’s Maven dependencies isn’t updated somehow. That’s why you may notice some errors in the created application.
  • Created-Application
  • Delete all undesired Java files under com.journaldev package.
  • Delete all undesired JSP files/folders under WEB-INF/jsp.

Modify Maven Dependencies

The Maven dependency file has got modified to serve two wanted features:

  • Add required dependencies for Struts2 and its relevant Portlet container Bridge.
  • Add all required Apache Pluto accompanies that would lead us for a healthy deployment onto Apache Pluto itself.

pom.xml

Employee Registration Views

As you may know, Apache Struts has considered JSP as a view technology, so we would add a three distinct files, register.jsp, success.jsp and failure.jsp.

According for Apache Struts Archetype, the files would be added under WEB-INF/jsp/employee folder.

register.jsp

failure.jsp

success.jsp

As you’ve noticed, we used a Struts Tag library for providing a form component and its relevant parameters. If you’re not so good with a Struts, it’s so good for you return here to read some articles about it as it’s very elegant framework.

Struts Tag library has provided a <url/> Tag for creating a Struts-based link. Struts-based link is a link that uses the Struts framework vocabulary like action.

One important thing you should notice that and it’s the action attribute for the form that’s referred for action it’s name is register. That name would be provided by the struts.xml file that would be discussed later on.

Register Action

It’s may be known for you that a Struts is an action-based framework. In that, every form submission should be delegated into action based class that’s implemented to be extended from DefaultActionSupport class. Following RegisterAction Struts action class.

Here’s detailed explanation for the RegisterAction class figured out:

  • Notice the class’s properties id, name, job and salary are analogous for these parameters’ names that are provided by the register.jsp page.
  • RegisterAction has overridden an execute method for serving the employee registration purposes.
  • Struts framework will binded all form’s parameters with those corresponding ones at the action class. Id, name, job and salary will be passed automatically.
  • RegisterAction has used the EmployeeDAO (The same utility class that we’ve impelemnted previously, that it would handle the all database manipulation code) for registering the employee instance.
  • In case the registration process has failed the Struts framework would take you into failure page. The page success.jsp is that one, the Struts framework will redirect you into as soon as the registration has finished successfully.

Struts Application Configuration & Portlet.xml

Struts.xml file is an XML-based file that used by the Apache Struts framework for getting connected the different components that you’re developed.

Wiring of mentioned actions within your <s:form/> with the Struts-action must be defined as an entries within struts.xml file. Following Struts configuration file required:

struts.xml

Integration of Apache Struts with the given Portlet container has been done through using of portlet.xml file. In that, a new init-param should be used for this purpose. Following the Portlet.xml file that’s used.

portlet.xml

Here’s detailed explanation for both of the Struts and Portlet configuration files listed above:

  • Your Portlets must be an instance of JSR286Dispatcher class.
  • We’ve defined three additional parameters, actionPackages, viewNamespace and defaultViewAction.
  • Defined defaultViewAction will be used once you’ve opened the Portal page that’s contained the deployed Portlet. As being index is the value, the index should be defined as an action in the struts.xml file.
  • Defined actionPackages will be used for determining the Java package that contains the Struts-actions classes.
  • Defined viewNamespace will determine the namespace for the actions configured for view mode. It’s also applicable to define actions for edit and help modes.
  • Struts configuration file has defined an register action that would be used as soon as the user has submitted the form’s register.
  • Defined register action has had two different results, success and failure are used to determine the next view that Struts framework uses after finishing the calling of execute against action class.

Deployment Descriptor

web.xml

EmployeeDAO & ConnectionUtility

Following below both of EmployeeDAO and ConnectionUtility that help you getting database connection and creating the user after then.

Registration Employee Demo

To get started showing your Portlet, you must logged into your Apache Pluto and navigating into JournalDev page. Typically, we have added a Portal page that’s contained for our developed Portlet. The process of creating a Portal page and add the Portlets upon it is discussed at Apache Pluto Introduction Tutorial.

Register-Employee-Page-1024x303

Summary

Apache Struts is an elegant framework that’s used for creating an MVC-based enterprise application. Apache has also provided you a ready made Portlet bridge that could be used for creating JSR-168/286 Struts-based Portlet. Contribute us by commenting below and find the downloaded source code.

By admin

Leave a Reply