Tutorial - Using Objective Html and Struts

Author Keith Wong, keithwong@optushome.com.au

Last updated: 23rd Feb 2002

This tutorial is now deprecated. Objective Html and Struts now has a much higher level of integration. See the Tutorial - Objective Html and Struts Advanced for the details.

Example Code

These are the files for this example.
customer.jsp  [source code]
CustomerForm.java  [source code]
CustomerLoadAction.java  [source code]
CustomerSaveAction.java  [source code]
struts-config.xml  [source code]

The binaries can be downloaded below (packaged in a war file). In Tomcat, simple place the war file under the "webapps" directory, it will automatically explode when Tomcat is started up. Make sure the objectivehtml-java-alphaX.jar is in the Tomcat $TOMCAT_HOME/lib directory. You should then be able to view the form using the URL http://localhost:8080/objectivehtml-struts. Replace the server address and port number if your computer is setup differently. If you experience any problems with Struts then check out the Struts website. Struts requires you to have a JAXP Xml Parser in your application classpath. Tomcat comes with a JAXP Xml Parser, by default it is not made available to other applications, see the Tomcat documentation for more details.
objectivehtml-struts.war

Introduction

This tutorial continues on from the first tutorial. If you have not gone through that tutorial then it is suggested you go back and read that one before proceeding.

The first tutorial used a primitive "Model 1" approach for processing and dispatching requests. In this next tutorial we'll show how Objective Html can be used with a more advanced "Model 2" approach for processing and dispatching requests. I have chosen to use Struts in this example because it is fairly well known MVC framework and I think it has a very neat system of processing and dispatching requests. Also given many developers may already have some knowledge of Struts I hope this may give them a better understanding of how Objective Html can fit into a MVC framework.

The tutorial assumes that the reader is familiar with the workings of Struts. If you are not then check out the Struts website it has some good tutorials on how to get started with Struts.

Once again we have our customer details form. The requirements of the form are the same as the first tutorial only its implementation has changed.

Code Walk-Through

CustomerForm.java

package mypackage.form;

The CustomerForm class is under a different package now. Under the "mypackage" package we have 2 new packages, "form" and "action". The "form" package will be used for storing our forms and the "action" package will be used for storing our Struts action classes.

		setAction("customersave.do");

This is the line has been added to our CustomerForm class. We set an "action" value for our form, this URL request will be used by Struts to determine what Action class to run.

	public boolean saveForm()

The saveForm() method now returns a boolean. It will return true if the save is successful, false if some validation failed.

CustomerLoadAction.java

This Action class is used to create a blank customer details form. When the user makes a request for a new form this Action is triggered off by the Struts dispatcher.

			CustomerForm objForm = new CustomerForm(); // our self defined form
			// store the new form in the session so we can use it later
			session.setAttribute("customerform", objForm);

In the code we create a new instance of the CustomerForm class and then save it in the session object. Notice how there is no Form class and there is no JavaBean class in the code. Using Objective Html our Form, Bean and html form code all exist in the one object.


		// everything went ok
		return (mapping.findForward("success"));

There's only one forward defined for this action "success".

CustomerSaveAction.java

This Action class is used to save the contents of a customer details form. When the user makes a request to save the form this Action is triggered off by the Struts dispatcher.

		// retrieve the previous form object
		CustomerForm objForm = (CustomerForm)session.getAttribute("customerform");

		// this method magically updates all form data
		objForm.updateData(request);

A CustomerForm object must already have been created if the Save Action is triggered, hence we can get the CustomerForm object from the session object. Next we call the magic method updateData(). This will now go and update all the fields of the form with the submitted data. Notice again there is no Struts Form class or Bean class. All our data is contained in the CustomerForm object.

			if (objForm.saveForm())
				return (mapping.findForward("success")); // saved successful

All we have to do now is call our user-defined method saveForm(). This will save the contents of the form. If the form saves successfully then we can forward to our "success" page, otherwise our "failure" page.

customer.jsp

	// retrieve the form object
	CustomerForm objForm = (CustomerForm)request.getSession().getAttribute("customerform");
	if (objForm != null)
	{

		// this method prints out the current state of the form out to the client
		objForm.printHtml(out);
	} // end if form exists

There is not much the Jsp needs to do. All it needs to do is retrieve the CustomerForm object from the session object and write out its contents to the output stream using printHtml().

struts-config.xml

    <!-- loading up the customer form -->
    <action    path="/customerload"
               type="mypackage.action.CustomerLoadAction">
      <forward name="success"              path="/customer.jsp"/>
    </action>

    <!-- processing the customer form -->
    <action    path="/customersave"
               type="mypackage.action.CustomerSaveAction">
      <forward name="failure"              path="/customer.jsp"/>
      <forward name="success"              path="/customer.jsp"/>
    </action>

Here we declare 2 action mappings for our form. The first action is for loading the form, this is triggered off when the requested URL is "customerload.do". We only have one forward for this action which is simply our customer.jsp page. The second action is our save form action. This will be triggered when then requested URL is "customersave.do". There are 2 forwards for this action, but both result in the same page in this case.

Conclusion

And thats it! We've successfully integrated Objective Html into the Struts framework. Now you have the best of both worlds. If anyone picks up mistakes in this tutorial please let me know.

Check out our next tutorial on separating page design and logic: Separating Page Design and Logic

SourceForge Logo