This document describes how to develop and deploy an application that runs and deployed as a portlet in IBM WebSphere Portal V5 using Jakarta Struts Framework (incorporated into IBM Struts portlet framework).
Introduction This article discusses how to use Struts Portlet Framework to implement a portlet written for IBM WebSphere Portal Version 5 (hereinafter referred to as WebSphere Portal). Struts is an Apache Jakarta project that provides a very popular open source framework for creating a web application. WebSphere Portal V5.0 provides Struts Portlet Framework to support the Struts application as a portlet deployment. The Struts Portlet Framework also supports Portal mode and devices in the Struts application.
If you have read the previous article describing the use of status mode to implement portlet, you will be familiar with the sample application used in this article. It follows the same design principle, but here is the use of Struts to develop and deploy. This application maintains a contact list that is specific to the user who logs in to Portal (address this entry). The contact list is stored in the database or memory. Users can individually view the contacts in the list for more detailed information. The edit mode of the portlet allows the user to add, delete, or modify contact information.
The focus of this article is not to develop a Struts web application; in the development of the implementation and configuration required for the Struts application as a portlet. You can learn how to create a portlet using Jakarta Struts Framework. In WebSphere Portal V5, Struts Portlet Framework came with Jakarta Struts 1.1.0.
The following products are used in the development and deployment of the sample application:
IBM WebSphere Portal Server Version 5.0 IBM WebSphere Application Server Version 5 A database for saving a contact entry (optional) IBM WebSphere Studio Version 5 (optional)
About the sample portlet application sample application implements a create / read / update / delete (crUd) operation for a set of persistent data. It maintains a list of contacts for specific users (such as simple address books). This application allows users to individually view contact entries or view contact entries in a list.
From a visual perspective, the implementation of the contact list application includes the following view (page):
Login View - Displays the view of the user login. Main View - Displays the contact list and takes options to select more information. Detailed View (Detail View) - Displays the details of the selected contact. Main Edit View - Displays the contact list and has an option to add, delete, and modify contact information. Add a contact view - Displays the table view to enter the information of the new contact. Modify the contact view (Modify Contact View) - Displays the form view to display the elements data of the existing contacts and allow updates.
This app does not provide an explicit page to delete an entry. Instead, to delete an entry, the user needs to select a contact entry from the main editing page, and then delete the process and refresh the main editing page. If there is an error, the system will display the appropriate message. No confirmation page and successfully executed page. Figure 1. Main view
Design portlet application
The previous article on the use of status mode implementation portlet is similar to the discussion below, because Struts implementation and status mode are basically similar.
Consider the operation and view of this application. You can see the operation as a user-level function or behavior you want to implement for the portlet. For example, you may need an action to add a contact or display a specific view. The view is the performance of the application after completing the user operation. For example, the result of adding a new contact can produce the result of the editing mode, the view of the portlet displays the contact home list.
Next, consider how you want the app to be visual. In this example, you need a main view page to list the contacts created. This page should allow users to choose a contact and obtain additional details about this person.
You want the contact list to each user; therefore, this portlet requires the user (user) object to be associated with the logged user.
You want to allow users to add, delete, and modify contacts in the list. You can provide a master-editing page to list contacts and support the deletion and modification of the selected entry. You don't need to delete a selected entry for the user to implement another view; you only need to allow users to delete a selected entry, and then use the modified contact list to refresh the primary editing page.
If the user selects a contact for modification, you can display a detailed page that can update any properties of this contact entry. After the user has completed the modification, you can restrict the main editing page.
Similarly, you can add a new contact from the main editing page. You can display another detailed page that users can enter and save properties of the contact entries. After the user is saved, you can re-display the primary editing page with the updated list.
Now you can determine the state shift available for this application. By applying appropriate operations to generate specific views, you can manage transfer. In the figure below, the view (JSP) is indicated by an ellipse, and the operation is indicated by a rectangle.
Figure 2. Status transfer graph
Implement portlet using Struts
This design requires you to achieve 7 operations. These operations are defined in the implementation provided herein:
MainviewAction DetailViewAction MAINEDITACTION AddContactivity deleteContactAction ModifyAction ModifyContactAction
From the state diagram you can see that the Add Contact view is actually direct steering from the main view to Add Contact JSP. Because no logic is required before rendering the Add Contact view, you can simply call the Add Contact JSP and configure it to global forward instead of operation.
Establish development environment
To launch a Struts-based Portle project in WebSphere Studio, you need to follow these steps:
Create a new J2EE Level 1.3 Enterprise Application Engineering. Import the Struts Portlet Framework. In the Standard WebSphere Portal V5.0 installer, you will find the portalstrutsblank.war file in the
If you don't use Studio, make sure that the following JAR files (and the Struts JAR file you import) are available in the build path. You can find these files in WebSphere Application Server and WebSphere Portal directories.
Before considering the implementation details of the category, beans, forms, and supported classes, make sure the implementation of the application configuration file. These files include:
Web application deployment descriptor, web.xml struts form verification XML file, validation.xml portlet deployment descriptor, portlet.xml struts configuration file, struts-config.xml
You need to update all of these files.
Web deployment descriptor - web.xml default web.xml file is provided as part of Portal Struts Framework. You need to change the deployment descriptor based on a particular application. First, "DISPLAY-NAME" is changed to the name meaningful name, such as Contacts List.
Building Struts 1.1 support in this framework divides the application into a module. When using the module, the configuration information and directory structure of a program are divided by module. Therefore, instead of a single struts-config.xml file for the application, you have multiple control files with independent directory structures, which are identified by the application URI. Struts Portlet Framework utilizes module support to provide Struts configuration mode and device (tag) distinction. The search path is used to determine the module and struts-config.xml file you want to use. It also determines the basic directory for positioning JSP. When searching for modules, add the following initial parameters Set the search path to consider tag names and patterns when searching modules.
init-param>
You created two modules for this app, one for editing mode, and another for help mode. The view mode is configured by the basic Struts configuration. These two modules are configured as initial parameters in such a form: / config /
init-param>
init-param>
Add two applications-specific initial parameters persist_to_db and DataSource to support persistence classes, which is part of the download. Setting this value to TRUE to cause deployment database proxies. If you use the database agent, you also need to set the data source. Set the value of the data source to the name of this data source. If you are using persistence in memory, you can ignore the value of the data source.
Finally, change the list of welcome files. Struts Portlet Framework allows you to specify an initial view of the device / mode. This can be specified by the list of welcome files.
welcome-file-list>
Portlet deployment descriptor - portlet.xml
You must also change the default portlet.xml file, which is provided by the blank Struts portlet.
Change
XML Version = "1.0" encoding = "UTF-8"?>
"- // ibm // DTD portlet application 1.1 // en" "portlet_1.1.dtd">
Major-Version = "1" minor-version = "0"> HREF = "Web-inf / web.xml # Contacts_list_portlet.contactslistportlet" Major-Version = "1" minor-version = "0"> cache> allows> Markup> supports> portlet> portlet-app> logage> config-param> concrete-portlet> concrete-portlet-app> portlet-app-def> The Struts Profile-Struts-Config.xmlstruts-Config.xml file contains the application definition of Struts Framework for control, navigation, and configuration settings (including the desired form bean). As mentioned above, when changing the web.xml file, the parameter value associated with each of the two application module definitions indicates where to start searching for a module-specific Struts profile. Therefore, you need to create a new struts-config.xml file, which will reside in / Web-INF / HTML / EDIT / and / WEB-INF / HTML / HELP /. These files will contain Struts configuration information specific to which they are made. First, change the Struts-Config.xml file in the / web-inf directory to the Structs configuration of the view mode. You need to create an action element in XML to support the operations identified in the transfer chart. There are seven specified operations. These operations can be defined below and will be displayed using the class names defined in the example implementation. You also specify the appropriate steering settings for each operation to indicate the JSP for rendering the request view. These rendering status are specified in the state transition chart. You can include these appropriate operations and steals in a module-specific configuration file. Next, create a new Struts-Config.xml file in the directory specified above. Struts-config.xml TYPE = "com.ibm.sample.contacts.struts.beans.contact" /> form-beans> You can also specify global steering, message source files, and verify XML file references. Global steering enabling the logical name associated with a URI pointing to JSP or an operation. Listing 2. View - /web-inf/struts-config.xml
"- // Apache Software Foundation // DTD Struts Configuration 1.1 // En" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> TYPE = "com.ibm.sample.contacts.struts.beans.contact" /> form-beans> global-forwards> PATH = "/ mainview" TYPE = "com.ibm.sample.contacts.struts.action.mainview" Name = "ContactForm" Scope = "request" Validate = "false"> Name = "mainviewpage" PATH = "/ main_view.jsp" /> action> Path = "/ detailview" TYPE = "com.ibm.sample.contacts.struts.action.detailview" Name = "ContactForm" Scope = "request" Validate = "false"> Name = "detailviewpage" Path = "/ detail_view.jsp" /> action> action-mappings> Processorclass = "com.ibm.wps.ports.struts.wpsRequestProcessor"> controller> Parameter = "com.ibm.sample.contacts.struts.nls.ApplicationResources" /> Value = "/ Web-inf / validator-rules.xml, /Web-inf/Validation.xml "/> plug-in> Struts-Config> Listing 3. Edit - /Web-inf/html/Edit/struts-config.xml Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Apache Software Foundation // DTD Struts Configuration 1.1 // En" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> TYPE = "com.ibm.sample.contacts.struts.beans.contact" /> form-beans> global-forwards> Path = "/ mainedit" Type = "com.ibm.sample.contacts.struts.action.maundage" Name = "ContactForm" Scope = "request" Validate = "false"> Name = "mainditpage" Path = "/ main_edit.jsp" /> action> PATH = "/ modify" TYPE = "com.ibm.sample.contacts.struts.action.modify" Name = "ContactForm" Scope = "request" Validate = "false"> Name = "editcontactpage" PATH = "/ edit_contact.jsp" /> Name = "deletecontact" Path = "/ deletecontact.do" /> action> Path = "/ modifycontact" type = "com.ibm.sample.contacts.struts.Action.modifyContactivity" Name = "ContactForm" Scope = "request" Validate = "false"> Name = "marinedit" PATH = "/ mainedit.do" /> action> PATH = "/ deletecontact" TYPE = "com.ibm.sample.contacts.struts.action.deletecontactivity" Name = "ContactForm" Scope = "request" Validate = "false"> Name = "mainditpage" Path = "/ main_edit.jsp" /> action> Path = "/ addcontact" TYPE = "com.ibm.sample.contacts.struts.action.addcontactivity" Name = "ContactForm" Scope = "request" Validate = "false"> Name = "AddContactPage" PATH = "/ add_contact.jsp" /> action> action-mappings> Processorclass = "com.ibm.wps.ports.struts.wpsRequestProcessor"> controller> Parameter = "com.ibm.sample.contacts.struts.nls.ApplicationResources" /> Struts-Config> Listing 4. Help - /Web-inf/html/help/struts-config.xml Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Apache Software Foundation // DTD Struts Configuration 1.1 // En" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> action-mappings> Processorclass = "com.ibm.wps.ports.struts.wpsRequestProcessor"> controller> Struts-Config> Struts Verification - Validation.xml You have not used Struts input verification in this application. You can delete the default verification specification in Validation.xml. form-validation> Directory Structure Change reviews, you have changed the list of welcome files in the web.xml file. This list is used to indicate the starting point of the defined module. Depending on the root of the web application, you need to create a directory for the editing and help mode, and put the index.jsp.jsp of each mode in these directories. Each module's struts-config.xml file (indicating Portal mode) does not display additional path information of the JSP file; therefore, you need to add an appropriate JSP for the module in each of these same directory. In the / html / edit directory, you have add_contact.jsp, edit_contact.jsp, and main_edit.jsp, and index.jsp files. In the root directory, you have Detail_View.jsp and Main_View.jsp. In the / html / help directory, we only have index.jsp files. The implementation of the operation is next, you will implement the operations required for this application. Details of each class are not discussed here because of the full implementation of these classes in the download section. You need to implement class to extend org.apache.struts.Action.Apache.Struts.Action.Action that is identified in the status shift map and defined in the struts-config.xml file. Check the code of the DeleteContactAction class that implements the ability to delete the contact. You can also see an abstract class of our operation class extension, so you can put universal code for implementing all operations classes into it. For example, you can add a code to verify that the user is subject to wait before executing this feature. Listing 5. DeleteContactAction class Public class deletecontactivity extends postloginabstract { Public ActionForward PerformAction (ActionMapping Mapping, Actionform Form, User, httpservletRequest request, httpservletResponse response Throws exception { // Get the userid and successd OID String Userid = user.getuserid (); Contact Contact = (Contact) Form; String Oid = contact.getSelectedContact (); // delete the Contact ContactHelper ContactHelper = ContactHELPER.GETInstance (); ContactHelper.Deletecontact (Broker, UserID, OID); // render the main edit page Return (Mapping.Findforward (main_edit)); } } Abstract operation class extension org.apache.struts.Action.Action and implementation method. This implementation ensures that the user is logged in, then calls the PerformAction method in the called operation class, transmitting the same parameters as the parameters of the user bean that have been transmitted to the execute method and the user bean retrieved during login verification. When you migrate your application to the Portal environment, you will only delete login operations and change this code to retrieve user objects from the portletRequest object. You can then change any application code to access the user object to use the WPS user object API. Listing 6. PostLoginabstractAction class Public Abstract Class AbstractActAction Extends Action Implements Constants { Public ActionForward Execute (ActionMapping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, HTTPSERVLETRESPONSE RESPONSE Throws exception { Try { // Initializations LOG = logfactory.getlog ("com.ibm.sample.contacts.struts.action"); Bundle = ResourceBundle.getBundle (Resource, Request.getlocale ()); // Initialize the Persistence Broker Broker = contactListBroker.getInstance (); IF (broker == null) { String persisttotb = getServlet (). GetInitParameter (DB); String DataSource = GetServlet (). GetinitParameter (Datasource); True.equalsignorecase (persisttoDB)) ContactListdbroker.initialize (Datasource); Else ContactListMembroker.initialize (); Broker = contactListBroker.getInstance (); IF (log.Indebugenabled ()) Log.debug ("Broker Initialized: Broker); } // Check for EXISTING WITH WITH Logged-in User HttpSession session = request.getations (); PortletRequest portletRequest = (portletRequest) Request; User = portletRequest.getuser (); IF (session == null || user == NULL) Throw new aimmessageexception (bundle.getstring); // Invoke the Action Class PerformAction Method Return PerformAction (Mapping, Form, User, Request, Response); } catch (exception e) { HttpSession session = request.getations (); Session.SetaTRibute (Exception, E); Return (Mapping.FindForward ("exception"); } } Public Abstract ActionForward PerformAction (ActionMapping Mapping, Actionform Form, User User, HttpServletRequest Request, HttpservletResponse response) throws Exception; } The implementation of the remaining operations is very similar, and if this application has a minimum logic process, then they are not complex implementation. For example, in the DeleteContactAction class, some operation class uses the service of the persistence class. The Persistence method is provided as part of the download implementation. Now you can view the ContactListBroker interface, which defines the functions required for the contact list processing. Agent class's implementation management data storage (in this case, or representing the data in the database, or data for data in the data). This function is required by this portlet, but it doesn't matter to this point of this article, you can view the implementation of a specific agent in the Download Sector. Listing 7. ContactListBroker interface Public interface contactListBroker { / ** * Get the Contact Information Given a Contact Number * @Param Owner java.lang.string * @Param Contactoid java.lang.string * @Return ContactForm * / Public ContactForm getContact (String Owner, String Contactoid) THROWS AIMEXCEPTION; / ** * CREATE A Contact * @Param ContactForm * / Public void savecontact (Contact Contact) THROWS AIMEXCEPTION; / ** * Delete a Contact * @Param Owner java.lang.string * @Param Contactoid java.lang.string * / Public void deletecontact (String Owner, String Oid) THROWS AIMEXCEPTION; / ** * Get the contact list. * @Param Owner java.lang.string * @Return List of ContactForm Objects * / Public List GetContactList (String Owner) THROWS AIMEXCEPTION; } Application Bean We define two beans for this application. We define a contact form bean in the Struts-Config.xml file. It expands org.apache.struts.validator.validatorForm, and includes the characteristics of the column mapped to the contact database table (with accessor). See the part of setting up the database for our sample table definition. Another bean is a helper class that provides a common function to add and delete contacts. These codes are also available in the download, which is no longer displayed. The application resource defines the message feature file in the struts-config.xml file. Our file is named ApplicationResources.Properties, which is located in a resource folder. This resource package provides a displayed string in the package that makes the application easier to convert. The rest of the Java Server Page (JSP) file is the JavaTM Server Page (JSP) file. As you are defined in the struts-config.xml file, based on the status transfer graph, you need to implement 5 JSP to present a page for the following purposes: Add Contact Information Edit Contact Information Main View Page Editor Page (Main Edit Page) Contact Details View Page (Detail Contact View Page) You also need to update the index.jsp file to call the initialization operation for each mode. Examples of some critical parts of this JSP file, that is, the part of the part of the main view and the content presentation of this JSP. This JSP needs a bean that exists during a session contains a list of contacts. You define several standard Struts tag libraries used in JSP. When the user selects a contact in the presented list, you want to continue the DetailView operation, transfer the selected contact OID as a parameter to the request form data. Listing 8. Main view JSP <% @ Taglib Uri = "/ Tags / Struts-Bean" prefix = "bean"%> <% @ Taglib URI = "/ Tags / Struts-HTML" prefix = "html"%> <% @ Taglib Uri = "/ Tags / Struts-Logic" prefix = "logic"%> <% @ Taglib Uri = "/ Tags / Struts-Portal-HTML" prefix = "portalhtml"%> <% @ Taglib Uri = "/ Web-INF / TLD / Portlet.tld" prefix = "portletApi"%>