The Model Component ( HelloModel.java )


The Model Component ( HelloModel.java )

In the previous section, you saw how the Action class interacted with the Model component HelloModel.java . In Listing 3.5, let's take a look at the HelloModel.java file itself.

Listing 3.5 The Struts Model Component Hello World! ( HelloModel.java )
 package ch03.hello; /**  * <p>This is a Model object which simply contains the name of the person we  * want to say "Hello!" to.<p>  *  * In a more advanced application, this Model component might update  * a persistent store with the person name, use it in an argument in a web  * service call, or send it to a remote system for processing.  *  * @author Kevin Bedell  */ public class HelloModel {     // --------------------------------------------------- Instance Variables     /**      * The new person we want to say "Hello!" to      */     private String _person = null;     // ----------------------------------------------------------- Properties     /**      * Return the new person we want to say "Hello!" to      *      * @return String person the person to say "Hello!" to      */     public String getPerson() {         return this._person;     }     /**      * Set the new person we want to say "Hello!" to      *      * @param person The new person we want to say "Hello!" to      */     public void setPerson(String person) {         this._person = person;     }     // --------------------------------------------------------- Public Methods     /**      * This is a stub method that would be used for the Model to save      * the information submitted to a persistent store. In this sample      * application it is not used.      */     public void saveToPersistentStore() {         /*          * This is a stub method that might be used to save the person's          * name to a persistent store if this were a real application.          *          * The actual business operations that would exist within a Model * component would depend upon the requirements of the application.          */     } } 

This is a very basic, simple Model component that is nothing more than a simple Java bean. The saveToPersistentStore() method is just a stub method that in a real application might store the person in a database of some kind.

Although this is a very basic example, it demonstrates a primary strength of the MVC framework. That is, the implementation details of Model components can be hidden from the rest of the Struts application.

If this Model component were changed to take the person property and store it in a database, the Action class might require no changes at all. The same could be true if HelloModel took the property and updated it through an EJB to a remote server or even if it sent it out through a Web service call.

Using Model components to hide the implementation details for interacting with remote systems is one of the keys to using Struts effectively.



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net