Extending Model Components
Because the Struts framework doesn’t provide a great deal of components for the model layer, extensions to these components are better discussed in other Java programming books. However, there are two classes that might be placed into the category of extensible model components. They aren’t the best representations of what a model component is, but they are responsible for holding model state.
The UserContainer and ApplicationContainer Classes
I’ve mentioned the
UserContainer
and
ApplicationContainer classes in previous chapters
without defining exactly what they are. These two classes are not
part of the Struts framework—I created them as part of the
example Storefront application. The purpose of these classes is to
store user and application-specific information in instances of these
classes, rather than in the HttpSession and
ServletContext objects, respectively.
One of the problems with storing data in the
HttpSession is that the interface to store and
retrieve data from the session object is not strongly typed. In other
words, the interface for any data is:
public void setAttribute( "permissionsKey", permissions ); public Object getAttribute( "permissionsKey" );
The client must be aware of the key at which the data is stored in order to put an object into or retrieve an object from storage. Some programmers prefer a more strongly typed interface instead:
userContainer.setPermissions( permissions ); userContainer.getPermissions( );
Here, the client ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access