File Components

In building your web application you will need to create a number of files. From scratch, the following should be a rough guide to getting it working. These include the Makefile, some mandatory "plumbing" code in Objective-C, GSWComponents to provide the pages and subpage elements of the web application, and run-time resources.

The Makefile

For a web application, a special makefile is needed: luckily it's very similar to makefiles you may have used for other kinds of project. There is currently no out-of-the-box way to have these files built for you, so you'll have to do it manually.

GNUmakefile

Initial makefile includes needed to set up the make environment should be:

include $(GNUSTEP_MAKEFILES)/common.make
include $(GNUSTEP_MAKEFILES)/Auxiliary/gsweb_wo.make

Note that the second include forces GNUstepWeb to use Apple's WebObjects naming conventions. Dropping the _wo in the included filename will enable GSW naming conventions. It is useful to decide which convention you plan to use in the application early on because changing later will involve changing the naming convention of class names in many files in your project.

The key variables used in a web application's makefile are:

GSWAPP_NAME

used to define the project's name in the makefile, replace the {PROJECTNAME} with the name in the variables listed below

{PROJECTNAME}_HAS_GSW_COMPONENTS

used to indicate that GSWComponents are present in the project. Normally, this is set to YES

{PROJECTNAME}_PRINCIPAL_CLASS

used to identify the main class of the web application

{PROJECTNAME}_RESOURCE_FILES

used to indicate the list of files that are needed in the runtime bundle

{PROJECTNAME}_OBJC_FILES

contains the list of Objective-C files needed to compile the web application

{PROJECTNAME}_COMPONENTS

lists the names of the components that will be used to create pages and other elements in the web application

The final makefile includes needed to set up the make environment should be:

The preamble and postamble files are optional includes and will not error if they don't exist. It's good practice to have these in place to allow compile-time modifications during the development process and also for others if you distribute the code.

A worked-out example of a simple GNUmakefile for a GSWeb application is:

The name of the project is SimpleDemo, and it contains GSWComponents (the basic building blocks of GSWeb), its principal class is SimpleDemo, there are no resource files for the runtime (there is an entry, but it's hashed-out), there are five Objective-C source files, and two components. You can see that the syntax allows you to break lines using the backslash \ character as the last on the line to assist readability, especially for long lists of values, like the OBJC_FILES.

For more information as to customising this file, as well as setting up compiler options for third party libraries and includes, see the GNUstep makefile manual.

Mandatory code

GNUstepWeb expects a small amount of glue code and several classes to be present at runtime. Initial implementation of a web application may leave these in boiler-plate form, but most real-world projects end up adding significant extra functionality through the extension mechanisms they offer.

The mandatory code is as follows:

main function

the runtime jumps to the beginning of the main function, which is used for initialisation of the the web application and can be used for initialisation of other resources such as C libraries etc

Session class definition

used for per-session initialisation in the web application, in addition to holding instance variables and methods for each session. This class should be literally named Session.

GSWApplication or WOApplication-derived class definition

used for web application instance-wide initialisation, and it may hold instance variables and methods across the entire instance.

Some worked out examples for the SimpleDemo web application follow. First SimpleDemo_main.m

The Session.m code above implements a small change in the normal behaviour of GNUstepWeb, forcing it to store session identifiers in cookies and not in the URL. There are good reasons not to do this in practice for some real web applications, but it does show the Session object may be used to change behaviour on a per-session basis.

The application can run quite successfully with very little other than this boilerplate code. Of course, real functionality requirements may mean creating or overriding methods in these classes (especially the -init methods), creation of new instance variables and so on.

Components

GSWeb uses a directory for every component. A component can represent a whole web page or just a part of it. It is easy to re-use pieces needed multiple times.

A component contains three files containing the HTML, the connections or bindings of your objects and additional information like the text-encoding used in the component. Those files are suffixed .html, .wod and .woo.

In the SimpleDemo web application there are only two components defined. In a real application it's usual to have custom components providing style sheet information, headers, footers, navigation bars and so on that are reused in the components that correspond to pages.

Main.wo

The Main component normally provides the first page when a connection is made to the web application. The name is not arbitrary, and normally every GNUstepWeb application contains a Main component. Our implementation will consist of five files:

Main.h the Objective-C class header for the Main component

Main.m the Objective-C class implementation for the Main component

Main.wo/Main.html containing the HTML markup

Main.wo/Main.wod containing the bindings of the dynamic elements that exist in the HTML to the application's objects, and

Want to know more?

The .woo files can seem a little mysterious in that they are only being used here to identify the text-encoding information of the component. In actual fact, they can be used for a number of other, quite powerful purposes including serialised information related to direct database transactions initiated from the .html and .wod files - in other words to avoid writing code!