Attention: this page describes functionality that is not yet available! Work in progress!
Struts 1.4 allows building portlet-like web components. A web component is an independent server-side object that is able to accept user input and to render itself.
A Struts Web Component is a combination of:
- one Action class that handles input events and updates component state;
- one or more JSP files that represent one or more component subviews;
- an optional form bean that holds input/output data.
Struts web component uses Action class to process incoming events, and employs JSP technology to render a view. The lifecycle of the components is fully managed by Struts. The components can be used in any application that employs JSP as rendering technology, not just in Struts-based application.
Struts utilizes two request processing concepts when dealing with web components:
- Traditional synchronous HTTP request/response cycle (Non-Ajax mode), and
In synchronous mode the browser submits input data to a fragment, the fragment updates its state if necessary, then the composite page is automatically reloaded by redirecting browser to the original page location. A request that follows pulls up web components and they render themselves.
In Ajax mode browser submits input data using an asynchronous request. Struts renders a view directly in response to this request, no page reloading is needed. HTML markup, returned by a component, is inserted into the composite page without full page refresh.
XMLHTTPRequest object, like some mobile browsers.
Use Case: Login Component
Consider a website's composite page that contains a login component. The login component authorizes access to member-only area of the website. A non-logged-in user must be presented with login form, a logged-in user should be able to log out. The screenshot from http://www.java.net website illustrates this use case.
A casual visitor sees a login form:
After a visitor logs in, he is presented with exactly the same page, the only difference is the login form replaced with logout form:
In a regular web application the login/logout module would be responsible for navigation to a proper location after processing user input. In the example above a user must be transferred to the same location that he was browsing before logging in. Therefore login/logout module should be tightly integrated with its parent page, or the parent page must process login/logout events itself.
With Struts you can build truly independent web components that bear no knowledge about a page they are included into. Furthermore, a composite page does not need to know about components contained in it. A web component should mean its own business, that is, handling its input and rendering its markup. Struts takes care about the rest. These boring tasks include:
- Calculating the location of a composite page (used to automatically reload a composite page).
- Calculating the location of a component (can be used as submission target).
- Save page/component locations between requests.
Building A Simple Struts Web Component
This guide explains Struts component functionality by building a simple login component. The Login Component has two states: "Logged In" and "Not Logged In", two corresponding views: "loggedin" and "notloggedin", and two input events: "login" and "logout".
If a user has not logged in yet, the Login Component stays at "Not Logged In" state. The "notloggedin" view displays login form with "username" and "password" fields and "Log In" button. The button submits login form, sending "login" event to the component.
When a user logs in, the Login Component switches to "Logged In" state. The corresponding "loggedin" view displays logout form that shows user name and "Log Out" button. The button submits logout form, sending "logout" event to the component.
We will build a synchronous component first, then we will convert it into a true dual-mode component.
Download WAR file
Download the sample: struts-components.war It also contains source code of business-related files (login action, form, JSP file) as well as suggested modifications for upcoming Struts 1.4 core.