EJBs Fit with Model Components


EJBs Fit with Model Components

As discussed earlier, Struts is a framework based on the Model-View-Controller (MVC) design pattern. It's also been pointed out that one of the great strengths of MVC is the flexibility you have for implementing Model components. This explains why Struts has tag libraries for building View components and Action classes for building Controller components, but no specific classes for extending or implementing Model components.

That means you'll most likely incorporate EJBs into your Struts project by accessing them as Model components from within your Action classes. You can access them directly from the Action class or you can build helper classes that hide the details of EJB access from the Action class ”there's no single right answer for how to incorporate them.

It should be pointed out that it's possible to access EJBs directly from the JSP files (View components) using scriptlets or via bean help classes or custom tags. In general, this practice is strongly discouraged. It's a misuse of the framework and will very likely require you to put code in your JSP file that really belongs in a Controller component.

For example, what if the EJB container is unavailable or throws you a RemoteException ? In that case, you must make decisions on what to show the user based on whether this error condition occurs ”and that decision is better made in an Action class where you have the ability to forward the user to an appropriate error page.

In addition, it's likely that you'll want to implement business logic to display different information or take different actions based on data you retrieve through the EJB. This logic is best put in the Controller components. Even if your initial requirements call for simply retrieving and displaying data through the EJB, you can be sure that your customers will change their minds over time and you'll have to implement new functionality by encoding business logic based on data in the EJBs. Do yourself a favor: Work with the framework and access the EJBs from your Action classes from the beginning.

So, why is the name of this section "EJBs Fit with Model Components" instead of "EJBs Are Model Components"? Basically, it's worded that way to emphasize all the flexibility you have. The best way to meet your system requirements might be to treat EJBs exactly as model components or it might not. Some best practice approaches are presented in this chapter, but what you do in the end might differ , depending on your need. This section gets you started, but your end design will vary based on what works for you.



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