Your First Stateful Project
So far, we have seen two of the technologies, D2W and ERRest, that Project Wonder offers for viewing and managing the data. In this tutorial, we will show how to do it with the "stateful" way of doing things. Stateful have been around since the beginning of WebObjects in 1996, so it's the oldest way of presenting data and constructing pages.
Stateful means that you don't have to worry about creating sessions and keeping track of data coming from HTML input fields and controls. In fact, D2W is also stateful.
In this tutorial, we are also going to use the Ajax framework, who is stateful too. We will replicate the functionalities of the two other tutorials, but by creating pages ourselves. The application will have the following pages:
- The main page will display a list of blog entries, with a link to see the blog entry.
- The main page will have a link to an "admin" page that will show a login form.
- After login, a list of blog entries with links to edit, delete and create blog entries will be show.
- We need a form to edit/create blog entries.
Let's start by creating a new project in Eclipse. You need to create a Wonder Application project type, and name it StatefulBlog.
Just like the D2W tutorial, you need to link the application with the BlogCommon, Ajax and H2PlugIn frameworks. To do so, right-click on StatefulBlog and select Build Path > Configure Build Path.
In the Libraries tab, click on Add Library. Select WebObjects Frameworks and click Next. Check Ajax, BlogCommon and H2PlugIn from the list and click Finish. The Libraries tab should look like this:
We are ready to code! Open the Components folder of the project, and open Main WO. In the Related view (bottom-right), you see that all related files of the component are listed, and we need to open the Java code associated with the component. To do so, in the Related view, double-click on Main.java to open the Java class into an editor.
In Main.java, we need some Java code to get the list of blog entries so that we can show that list into the component. The following code will do what we need:
import com.webobjects.appserver.WOContext;
import com.webobjects.eoaccess.EODatabaseDataSource;
import com.webobjects.eocontrol.EOEditingContext;
import er.extensions.batching.ERXBatchingDisplayGroup;
import er.extensions.components.ERXComponent;
import er.extensions.eof.ERXEC;
public class Main extends ERXComponent {
public Main(WOContext context) {
super(context);
EODatabaseDataSource dataSource = new EODatabaseDataSource(editingContext(), BlogEntry.ENTITY_NAME);
ERXBatchingDisplayGroup<BlogEntry> dg = new ERXBatchingDisplayGroup<BlogEntry>();
dg.setNumberOfObjectsPerBatch(20);
dg.setDataSource(dataSource);
dg.setObjectArray(BlogEntry.fetchAllBlogEntries(editingContext(), BlogEntry.LAST_MODIFIED.descs()));
}
private EOEditingContext _ec;
private EOEditingContext editingContext() {
if (_ec == null) {
_ec = ERXEC.newEditingContext();
}
return _ec;
}
}