Chapter 10. Connecting to the Data
Introduction
You often hear Struts referred to as a
model-view-controller (MVC) framework for web
applications. Struts can help you build web
applications that separate the data (model) from the presentation
(view); however, Struts doesn’t provide a framework
for the model. Rather, it supports custom Action
s
which broker the interaction between the view
and model.
This chapter contains several recipes that offer different approaches to interacting with your application’s model. You will find solutions related to accessing relational data by using JDBC and the popular Hibernate object/relational mapping framework. This chapter shows you ways to create pluggable interfaces for your business services. Finally, this chapter demonstrates some techniques for working with XML-based data stored in flat files.
10.1. Accessing JDBC Data Sources from an Action
Problem
You want to access a data source directly from a Struts
Action
.
Solution
Don’t do it! Only access the data source from within your application’s model.
Discussion
Though Struts is referred to as an MVC framework for web
applications, Struts doesn’t attempt to provide the
model of that paradigm. Struts provides only the bridge between the
view and the model, Struts Action
s. An
Action
shouldn’t access the
database; an Action
shouldn’t
need to know how the data is stored and accessed. Instead, an
Action
acts as a thin façade to your business services, marshalling data from the view to the model and back. ...
Get Jakarta Struts Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.