Struts HOW-TO database access

Source: Internet
Author: User

Struts HOW-TO Series
++ How to access the database ++
[Access a database]
In the design process of a Struts application system, it is best to use the web/presentation layer and your business logic class (including the layers for all data access operations) define an Action class as a small adapter (thin adapter ).
Therefore, you can first define some business APIs, which are simple Java classes. You can pass some parameters to these objects and return a collection of Java Beans or Java beans from these objects. This Action class is responsible for calling these objects and passing the returned values to the web/presentation layer.
Generally, you can create an Action class for every business method/Business API you need to call. Ideally, all the database access code is encapsulated in these business API classes, so Struts does not know the persistent layer you are using) (I don't even know that you are using the persistent layer ). It only needs to pass a primary Key or a query parameter, and then process the returned result bean or bean set. In this way, you can reuse these business APIs in other application environments. You can also perform a standalone test on these business APIs that are independent of the Struts or HTTP environment.
At the beginning, the simplest way is to design a scheme and define an Action class for each business API entry point. After your experience, you can use DispatchAction to combine these Action classes. You can even define a simple "Framework" Action to call all these business classes. You can find the ProcessAction designed by Scaffold in the contrib directory, which is a complete implementation of the "Framework" Action. This solution can use fewer Action classes, but you must have a deep understanding of the underlying implementation of Struts and MVC frameworks. Don't be afraid to define too many actions at the beginning. The Struts configuration scheme gives you full freedom to refactor your design in the future, because you can flexibly change your Action class, without affecting the application.
Ideally, the business logic layer should encapsulate all data access details, including the acquisition of database connections. However, some applications are designed to require callers to obtain database connections from a DataSource object. In this case, the Struts DataSource Manager enables you to configure these DataSource resources as needed.
The Struts DataSource manager is defined in the Struts configuration file (Struts-config.xml. This manager can be used to distribute and configure any connection pool that implements the javax. SQL. DataSource interface ). If your DBMS or container has a built-in connection pool that meets these requirements, you can choose it first.
[Implementation of the Public connection pool of Jakarta-BasicDataSource]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.