Use struts portlet to implement page navigation in the portal application

xiaoxiao2021-03-06  72

This article describes how to organize web navigation modes using multi-module support built in the Struts Portlet framework.

introduction

Struts is a very popular open source platform foundation, which is designed to make complex web applications more easily. Struts Framework Support Model - View-Controller (MVC) model, and proposes many important application design and implementation that requires attention. In order to be able to implement page navigation in the portal application, WebSphere® Portal V5 provides some tags that can be used for JavaServer Pages (JSP) when developing typical portlets and struts portlets. This article focuses on discussing the Struts framework to create multi-module capabilities, and introduce you to how to use JSP implementation page navigation methods through a sample struts portlet application.

We will pay attention to the following two complementary methods:

How to use a JSP tag available in WebSphere Portal to implement page navigation from JSP

We will see a sample application, including the screenshot of the development environment and portal resources, and code samples for page navigation methods.

How to organize web navigation schemes using multi-module support built into the struts portlet framework

We will see some code samples that explain how to use the page navigation feature to perform multiple Struts modules converted in the portal environment.

This article assumes that you have basically learned the Struts application, including how to create a Struts application in WebSphere Studio Application Developer, how to deploy Struts as portlets, and also understand the portal resources in WebSphere Portal. (For more information on these aspects, see Resources.)

Used the following products when developing and deploying our sample applications:

WebSphere Portal v5.x

WebSphere Application Server V5.x

WebSphere Studio Application Developer V5.0.1 (hereinafter referred to as Application Developer) is supported by Struts Version 1.1.

The development tasks used to implement page navigation described herein can be used in both standard portlets and struts portlets.

Application overview

In the sample application, we will use three portlets located in three different pages. When a user selects a link in a portlet, the user will be directed to another page, which resides another portlet. The examples we developed and mentioned throughout the article will use the multi-Struts module to mark the WebSphere Portal page navigation tagged into the JSP file in each module itself in the portal environment. Figure 1 summary shows how the sample application works; in order to understand these methods, we deliberately simplify sample applications. Behind this article we will provide details of the application.

Figure 1. Advanced view of the sample application

Build a navigation structure

WebSphere Portal provides its own JSP tag for portlet JSP. One of them, is used to create a page link to anywhere in the portal. We use this mark, contentNode = "id | name" to create the URL of different pages to build the navigation structure. This attribute uses a unique identifier (name of the content node) to specify a page that can be found from it. (For more information on this mark.) Select Administration => Portal Settings => Custom Unique Names in WebSphere Portal. We use Custom Unique Names to manage portlets to get the unique identifier for the portal resource specified by the ContentNode property. Administrators can use the Custom Unique Names portlet to view the unique identifier and assign the portal resource (let people read) custom name. Figure 2 shows the unique identifiers and custom names of these configured sample pages:

Struts_test

Struts_Receiver

Struts_receiver_2

Figure 2. Custom Unique Names portlet

In this example, the unique identifiers assigned to these pages are 6_0_lt, 6_0_p1 and 6_0_ip, respectively. (Your value may be different.) Also created the same portal resource struts_test, struts_receiver, and struts_receiver_2 portlet page on another machine residing WebSphere Portal. For the convenience of reading and work, we also configure custom names for these sample pages and use these custom names in the JSP file (instead of the only marker).

In the Portlet JSP tag body, you can write the URL directly to the output stream using the <% wpsurl%> script variable. In this example, we use the following JSP code to create a link from a JSP page to the Struts Receiver page:

Any Text

Portlet JSP tag belongs to the Engine.TLD tag library, which is used by the portal server engine. To make the portal JSP tag can be used in our JSP, we add tag libraries to the Taglib in the first position of the JSP page. As shown in the following code, through the prefix WPS, all the engine.TLD tags in our sample can now be used:

<% @ Taglib URI = "/ Web-INF / TLD / ENGINE.TLD" prefix = "wps"%>

Sample pages and portlets Configuring the page navigation bar of the sample application consists of three portlets (the long names here help to emphasize the page navigation process):

OverView struts portlet:

Two links are used to display users to pages on a page containing phone numbers or address information, which is located in the struts_test page (custom name: struts_test_page), as shown in Figure 3:

Figure 3. Overview struts portlet in the struts_test page

Receive Phone Numbers Request Struts Portlet:

Display the user's phone number, located on the Struts_Receiver_2 page (custom name: struts_receiver_2_page), as shown in Figure 4:

Figure 4. Receive Phone Numbers Request Struts Portlet in Struts_RecEcess_2 page

Receive Address Request Struts Portlet:

Display the user address information, located on the page (custom name: struts_receiver_page), as shown in Figure 5:

Figure 5. Receive Address Request Struts portlet in the struts_receiver page

Keep in mind that all tasks described so far can be used to develop regular portlets or struts portlets. Next we will use the portal JSP tag in the multi STRUTS module to implement page navigation.

Use multiple module support in Struts

Struts Version 1.1 allows us to split a single Struts application into multiple modules, each module has its own operation, JSP page, and struts profile (Figure 6). Implementing a multi-struts module in the Struts application requires the following steps:

Create a standalone struts profile and the struts operations and JSPs for each application module.

Configure the web deployment descriptor (web.xml).

Switch from one module to another module.

Although the Struts application is easy to apply, you need to perform other steps if you want to run the Struts application in your portal environment. (See Resources).

Figure 6. Multimode support in the Struts application

Create separate Struts modules and files

For our sample applications, we created four Struts modules, as well as their own struts profiles, behaviors, and JSPs. These Struts modules are:

Address

Phone-numbers

Overview

DEFAULT.

We also configure web.xml and portlet.xml files and use the Default module to perform conversion from one module to another module.

Next, we will explain the changes needed for our files based on the needs of the implementation.

Develop page navigation in the Struts module

We implement page navigation in the following three modules:

Overview

Address

Phone-numbers.

In addition to some subtle differences, each page navigation development task in these modules is almost the same. Therefore, we will explain the task of the page navigation of the OverView module, and then describe the difference between the other modules to implement the task and this. In addition, we will also explain the development process specific to the Struts module, which is converted for the Default module. In our implementation, the DEFAULT module is used to hand over the control to the next appropriate module; do not need page navigation. However, since it launches other modules to perform module conversion and is a main part of the navigation structure, we also include it here. In Application Developer, we create a web project that supports Struts Version 1.1 and then creates files for development tasks, as shown in Table 1 below.

Table 1. List of development components

The following is a brief description of the components shown in Table 1:

JSP file:

We have developed five JSP files:

INDEX.JSP and FAILURE.JSP files for default modules;

Address.jsp file for the Address module;

Phone-number of the Phone-Numbers module;

Overview.jsp file for the Overview module.

operating:

We created four Struts operations in the com.test.strutstest.Am.Actions package. Their classes are:

DisplayAction

DisplayAddressInfoAction

DisplayPhoneInfoAction

OverviewAction.

Form Bean:

We create two form beans in the com.test.strutstest.forms package. Their classes are:

Userformbean

OverviewFormbean.

Back end bean and model class:

We use a backend bean class (Userbeanhelper) in the package. (The backend bean contains stub data, does not communicate with the real backend.)

Struts configuration file:

We have developed four struts-config.xml files, each module uses:

WEB-INF / STRUTS-Config.xml for Default Modules

WEB-INF / Address / Struts-Config.xml for Address Modules

WEB-INF / phone-NumBers / Struts-Config.xml for Phone-Numbers modules

WEB-INF / OVERVIEW / STRUTS-CONFIG.XML for OverView modules.

Other profiles:

You need to modify the web.xml and portlet.xml file when deploying the Struts application as a portlet.

Each of the above configuration files contains the following request processor:

Processorclass = "com.ibm.wps.ports.struts.wpsRequestProcessor">

Figure 7 shows the location of the development member in the Application Developer development environment:

Figure 7. Location of the development component in WebSphere Studio Application Developer

Configure the Default module

To start the Default module and see how it is converted between the Struts module, you need to perform the following steps: The Default module is started when WebSphere Portal calls the index.jsp file, this is specified in the tag:

Index.jsp

When you call index.jsp, specify the logic forward name DISPLAY in the Logic Struts tag:

Define the logic forward name in the Struts-Config.xml file of the DEFAULT module as global forwarding:

In the section, the logic global forwarding the context relationship path path = "/ display.do" The context relationship path of the operation element in the section Path = "/ display" path is matched. This match can start the DISPLAYACTION operation. Operating mapping section (including elements with path and type properties) as follows:

TYPE = "com.test.strutstest.actions">

...............................................

In order to enable module conversion, configure the struts-config.xml, portlet.xml, and config.xml files for the Remaining module:

Configure the Struts-Config.xml file of the default module. We use DisplayAction as our operational and four logic forwarding:

TYPE = "com.test.strutstest.actions">

ContextRelative = "true"

Path = "/ failure.jsp" />

ContextRelative = "true"

Path = "/ address / start.do" />

ContextRelative = "true"

Path = "/ phone-number / start.do" />

ContextRelative = "true"

PATH = "/ overview / start.do" />

Configure the portlet.xml file to make it compliant with the configuration in the Struts-Config.xml file of the DEFAULT module. In particular, each of the four logical forwarding names in the Struts-Config.xml file of the default module is matched to the View.ForWard configuration parameter value set in the portlet.xml file. Table 2 shows this mapping relationship:

Table 2. View.Forward configuration value Configurable value for forwarding names

Configure a web.xml file to define different modules. We use config initialization parameters to tell our default module; however, for each of the other modules, we list a initialization parameter called config / module, where Module is our module name. E.g:

You can define the default module like this:

config

Web-inf / struts-config.xml

Other modules can be defined like this:

config / address

Web-inf / address / struts-config.xml

config / phone-number

Web-inf / phone-number / struts-config.xml

config / overview

Web-inf / overview / struts-config.xml

(For other module configurations and the necessary web.xml configuration settings, see the reference.)

Perform standard updates to Web.xml and portlet.xml files to deploy the Struts application as portlets. (To get more information, see the sample web.xml and portlet.xml files for the reference information and download part.)

As configured in the section in the Struts-Config.xml file of the default module, module conversion is done in the DisplayAction class. To determine which module of WebSphere Portal calls, you can do the following steps:

When you call the displayaction class's Execute () method, access is the View.Forward parameter set in the portlet.xml file: portletsttings portletSettings = Request.getPortletSettings ();

String ViewForward = PortletSettings.getaTAttribute ("View.Forward");

For our portlet, the View.Forward parameter has the following values:

Table 3. View.Forward configuration parameters of the sample portlet

If ViewForWard is empty, this variable is set to Failure in the Execute () method.

The forwarded mapping of configured ViewForward values ​​is retrieved from the struts-config.xml file:

ActionForward Forward = New ActionForward ();

Try

{

Forward = mapping.findforward (forwardname);

}

Catch (Exception E)

{

...........................................

}

The last step in the execute () method returns an instance of ActionForward (Forward):

Return (Forward);

Configure other struts-config.xml files. Determine the result according to the module in the DisplayAction class, if the View.Forward parameter value is:

Address:

The Start path that matches in the

TYPE = "com.test.strutstest.actions.displayaddressInfoaction"

Name = "Userformbean"

Scope = "Request">

Phone-number:

The Start path that matches in the section of the Struts-Config.xml file of the default module makes the DisplayPhoneInfoAction call in the Phone-Numbers module. According to the configuration in the web.xml file, this process needs to use the Phone-Numbers / Struts-Config.xml file. The phone-number file contains the following settings:

Type = "com.test.strutstest.Actions.displayphoneInfoaction"

Name = "Userformbean"

Scope = "Request">

Overview:

The Start path that matches in the

TYPE = "com.test.strutstest.actions.overview"

Name = "overviewFormbean"

Scope = "Request">

Failure: The Failure.jsp is launched under this parameter value according to the configuration in the Struts-Config.xml file of the default module.

So far, we have created the files listed in Table 1, and modify them according to our example needs. Now we can start writing code to implement page navigation using the OverView module. The following sections will guide you through the specific steps.

Develop overview module

To generate Overview.jsp files, OverViewAction classes, OverViewForMBean classes, and overview / struts-config.xml files to develop overview struts portlets, follow these development tasks in the operation class and JSP files:

Once the control is transferred from the Default module to the OverView module, the Execute () method in the OverViewAction class performs the following tasks:

Access the OverViewFormBean form bean and set the following properties:

Try

{

String forwardname = null;

IF (Form == Null)

{

Form = (ACTIONFORM) Request.GetaTribute ("overviewformbean");

}

// SET-UP FORM Bean Fields

OverviewFormBean Formbean = (overviewFormbean) Form;

FORMBEAN.SETPHONENENUMBERSLINKNAME ("List Phone Numbers");

FORMBEAN.SETADDRESSLINKNAME ("Address Information");

................................................ .

Store this form bean in the correct scope:

Try

{

// store the bean in the correct scope

IF ("REQUEST" .Equals (mapping.getscope ()))))

Request.setttribute (mapping.getname (), formbean;

Else

Session.SetAttribute (mapping.getname (), formbean;

}

Catch (Exception E)

{

................................

}

Find maps in Overview / Struts-Config.xml and return an actionforward type:

String forwardname = null;

ActionForward Forward = New ActionForward ();

.......................................

// if no errors occur then ing for "success"

Try

{

Forward = mapping.findforward (forwardname);

IF (Forward! = NULL)

{

.....................................

}

Else

.....................................

}

Catch (Exception E)

{

.................................

}

Return (Forward);

If an error does not occur during the processing of the operation class, the forwarded name Success will allow Overview.jsp based on the configuration in our overview / struts-config.xml file. Overview.jsp Display two links, pointing to other portlets - Receive Phone Numbers Request Struts portlets and Receive Address Request Struts portlets. We use the page navigation method and the Struts tag to implement this feature:

The Taglib instructions in the first place in the JSP page are used to make all Engine.TLD tags available for the remaining JSP pages with prefix.wps.

<% @ Taglib URI = "/ Web-INF / TLD / ENGINE.TLD" prefix = "wps"%>

Use the ContentNode parameter in the Portlet tag to specify the unique identifier or custom name of the page that resides other portlets. In order to start the page navigation of the Receive Address Request Struts Portlet, we use the custom name struts_receiver_page:

To start the page navigation to the Receive Phone Numbers Request Struts Portlet, we use the custom name Struts_Receiver_2_page:

Property = "phonenumberslinkname" />

This way we get the custom name of the page from My Pages and the Unique Name list, as shown in Figure 2.

In the Overview module, we use , and struts tag. Implement these tags require the following steps:

Declare the HTML and Bean prefix:

<% @ Taglib Uri = "/ Web-INF / TLD / STRUTS-HTML.TLD" prefix = "html"%>

<% @ Taglib Uri = "/ Web-INF / TLD / STRUTS-bean.tld" prefix = "bean"%> struts tag: If there is available in the user session, it will be appropriate Language properties to present HTML elements.

Struts Tag: All HTML form processing used in Struts. The FORM element must always contain the action attribute. The Action attribute must conform to the effective operation in struts-config.xml. In the overview.jsp file, we use the FORM element and its action attribute:

START property maps the following operations in OverView / Struts-Config.xml file:

TYPE = "com.test.strutstest.actions.overview"

Name = "overviewFormbean"

Scope = "Request">

The Struts tag connects the form field and the form bean in Struts. Positioning (or generated, if necessary) form bean based on a form bean specification (or generated, if necessary) based on the form bean specification for the associated actionmapping. The form bean defined in the overview / struts-config.xml file is UserformBean:

Struts tag:

This general marker is used to output characteristic values ​​from the bean. In the overview.jsp file, it displays to display the text data set in the UserformBean form bean. We use this tag to display certain features of userformbean, as shown below:

Developed the remaining modules

Developing the Address and Phone-NumBers modules and developing the OverView module is very similar, different from:

The Address module implements the Receive Address Request Struts portlet is:

Address.jsp

DisplayAddressInfoAction Class

Userformbean Form Bean Class

Userbean backend bean classuserbeanhelper model classuserbeanhelper model

Address / Struts-Config.xml file.

The Phone-Numbers module implements the Receive Phone Numbers Request Struts portlet is:

phone-number.jsp

DisplayPhoneInfoAction Class

Userformbean Form Bean Class

Userbean backend bean class

Userbeanhelper Model Class

Phone-numbers / struts-config.xml.

For address and phone-number modules, we are using the backend UserBean Bean class and the UserBeanHelper model class. Both classes need to use the stub data to simulate user information, and the stub data is using our module classes in the backend bean. Set. Both modules use the same form bean.

To develop the Address module in the DisplayAddressInfoAction class is:

Once the control is transferred from the Default module to the Address module, the Execute () method of the DisplayAddressInfoAction class performs the following tasks:

Access Form Bean - UserformBean and set the following properties:

Try

{

String forwardname = null;

IF (Form == Null)

{

Form = (ACTIONFORM) Request.getaTribute ("Userformbean");

}

// SET-UP FORM Bean Fields

Userformbean formbean = (userformbean) Form;

SetFormbean (FORMBEAN);

................................................ .

Store this form bean in the correct scope:

Try

{

......................................

// store the bean in the correct scope

IF ("REQUEST" .Equals (mapping.getscope ()))))

Request.setttribute (mapping.getname (), formbean;

Else

Session.SetAttribute (mapping.getname (), formbean;

}

Catch (Exception E)

{

................................

}

Find mappings in Address / Struts-Config.xml and return an object of an ActionForward type:

String forwardname = null;

ActionForward Forward = New ActionForward ();

.......................................

// if no errors occur then ing for "success"

Try

{

Forward = mapping.findforward (forwardname);

IF (Forward! = NULL)

{

.....................................

}

Else

...................................}

Catch (Exception E)

{

.................................

}

Return (Forward);

Call SetFormBean (UserformBean Formbean). This method gains a single instance of the model:

Userbeanhelper userbeanhelper = userbeanhelper.getinstance ();

This instance is implanted into backend data, sets the instance object of the userformbean, and returns the object using the following method calls:

Userformbean userformbean = userbeanhelper.getuserformbean ();

This method is to set the properties of the FORMBEAN together with the appropriate Getter and Setter methods, as shown below, where the objects returned from the model are used:

Public void setFormBean (UserformBean FORMBEAN)

{

Userbeanhelper userbeanhelper = userbeanhelper.getinstance ();

Userformbean userformbean = userbeanhelper.getuserformbean ();

// There Two Parameters Are Just Text Values ​​To Be Displayed On The Browser

FormBean.SetAddress ("Address");

FormBean.SetUserName ("UserName");

// setting the formBeans Object Attributes with data obtained from the backend

FORMBEAN.SETADDRESSVALUES (UserformBean.getaddressValues ​​());

FORMBEAN.SETUSERNAMEVALUES (UserformBean.getuserNameValues ​​());

}

If there is no error in the operation class processing, the forwarding name Success will start the address.jsp file to be launched based on the configuration in our Address / Struts-Config.xml file.

After appropriate modification, the above steps can be repeated to develop the Phone-Number module.

Webpage navigation

The address.jsp and phone-number numbers.jsp files for the Receive Address Request Struts Portlet and Receive Phone Numbers Request Struts Portlets are performed using the Overview Struts portlet and the Struts tag:

The front end of the JSP page uses the Taglib tag library instruction to make all Engine.TLD tags can be used for the remaining JSP pages with Prefix.WPS.

<% @ Taglib URI = "/ Web-INF / TLD / ENGINE.TLD" prefix = "wps"%>

Use the contentNode parameters in the Portlet tag to call the page navigation of the Overview Struts portlet. This requires a custom name or unique marker of the Struts_Test page where the OverView Struts portlet is located. We use the custom name struts_test_page, which is obtained from my Pages and the only name list, as shown in Figure 2.

Go to overview struts portlet

In our implementation, the Struts tag is used in address.jsp and phone-numbers.jsp files. This tag traverses the elements in the collection, which can be an attribute, or a property characteristic:

Declare the logic prefix:

<% @ Taglib URI = "/ Web-INF / TLD / STRUTS-LOGIC.TLD" prefix = "logic"%>

Use the Struts tag; we use the UserNameValues ​​character string in the UserformBean action form. The following code shows the user name set in this operational form:

Property = "UsernameValues" indexid = "index">

Establish development environment and install sample code

To create your own development environments in Application Developer, use the information provided in a simple Struts application in WEBSPHERE Portal 4.2 development struts applications and using WebSphere Studio V5. You need to import the Strutstest.war file of the download section into Application Developer and use all of its JAR files. Installing the sample code in WebSphere Portal does not require any special installation steps; you can use the WAR files you can use, which is willing to use which one can be used.

Conclude

This article describes the page navigation features provided by WebSphere Portal through a portlet mark, and also describes the multi-module support provided by the Struts portal framework. In our sample implementation, we created the portal page where the application is located, and then merge the portlet JSP to merge in the appropriate JSP. We use the page's custom name in JSP instead of the unique marker assigned by the portal server, which makes our JSP deployment relatively independent.

Although our example is not a very complex application, the framework provided by Struts can process large and complex application flows well. Multimode supports a single file group for each module that supports parallel development, and also provides better file organizations, multi-module support can coordinate work with the page navigation method provided by WebSphere Portal JSP tags.

转载请注明原文地址:https://www.9cbs.com/read-86989.html

New Post(0)