Web Services Fit with Struts Model Components


Web Services Fit with Struts Model Components

Note

Material contained in this section is largely repeated from the previous chapter. This is because EJBs and Web Services are accessed from Struts in a similar manner.

This duplication is intentional to allow this chapter to read as standalone by readers who aren't interested in reading the previous chapter.

If you read the previous chapter, we recommend skimming these sections both as a review and because the material isn't completely identical.


Struts is a framework based on the Model-View-Controller (MVC) design pattern. One of the strengths of MVC, as you recall, 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 to extend to implement Model components.

That means you'll most likely incorporate Web Services into your Struts project by accessing them as Model components from within your Action classes. You may access them directly from the Action class or you may build helper classes that hide the details of the Web Service from the Action class.

Although it's possible to access a Web Service directly from the JSP files (View components) using scriptlets or via bean helper classes or custom tags, this practice is strongly discouraged. Such an approach is a misuse of the framework and will likely require you to put code in your JSP file that really belongs in a Controller component.

For example, what if the Web Service is unavailable or throws an Exception when you access it? If this happens you will need to make decisions on what to show the user based on whether or not the Exception is thrown. This decision is better made in an Action class in which you have the ability to forward the user to the appropriate error page.

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

So, why is the name of this section, "Web Services Fit with Struts Model Components" instead of "Web Services Are Struts Model Components"? Basically, the wording is to emphasize all the flexibility you have. The best way to meet your system requirements might be to treat the Web Service exactly as a model component, 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