Home > Tutorials > Examples > Chat Application |

Apache Struts 2 Documentation
Chat Application
Edit Page Browse Space Add Page Add News

The application works like a mini-BBS. Users login to the application with a nickname. The user session is saved in a session scoped component. Once logged in, they can leave quips or messages.

## Action Classes

`LoginAction` extends `ActionSupport`, which has default implementations of some of the basic action methods and provides some validation support.

The two main methods we are concerned with in `ActionSupport` are `validate()` and `execute()`.

## Validation

Validation is performed on your Action class if it implements `Validatable` (`ActionSupport` does), and your DefaultWorkflowInterceptor is activated on that action. The default configuration includes the workflow interceptor.

## Execution

`execute()` returns a String. This String will be used to determine which result is used.

The framework provides some default return Strings, namely:

For example, lets take a look at the relevant part of our `struts.xml` configuration for LoginAction...

struts.xml fragment

If `execute()` returns a String of "success", the result with attribute "success" will be used. If `execute()` returns a String of "input", the result with attribute "input" will be used.

You can define your own return results. For example:

We need only configure a result for the "resetPassword" name:

## Context Variables / Mapping

LoginAction.java

`LoginAction` has a bean property `loginName`. This property will be set automatically by Struts 2 from forms containing a `loginName` element.

Also, the bean property is available to your views. In Velocity, this accessible via the VelocityContext.

which is mapped to `getLoginName()` in our `LoginAction` class.

You can map any other object you wish. For example, I could have a `User` object:

We add a `user` property to an action class:

In our Velocity template we can access `User` properties as expected:

## Components

components.xml

Two components, one to hold the application scoped chat messages, another to hold the user's session information (login account name, etc.) For all practical purposes, you can replace the application scoped component with a database. i.e. instead of reading/writing to the component, read/write to the database.