Struts How-to Series Database Access

zhaozj2021-02-16  62

Struts how-to series

How to access database

[Access a database]

In a design process of a Struts application, it is best to define an Action class in the middle of the web / representation layer and your business logic class (including layers of all data access operations), as a small Thin adapter.

So, you can define some business API (Business API), which is a simple Java class. You can pass some parameters to these objects and returns a collection of Java Beans or Java Beans from these objects. This Action class is responsible for calling these objects and passes the values ​​returned to the web / representation layer.

Typically, you can create an Action class for each business method / business class API you need to call. Ideally, all database access code is packaged into these business API classes, so Struts does not know the persistent Layer you are using (even don't know that you use a persistent layer). It only needs to pass a primary key (key) or a query parameter, then process the return result bean or bean collection. This way, you can multiplex these business API classes in other applications, you can also test these business APIs independent of the Struts or HTTP environment.

At the beginning, the easiest way is to design a 1: 1 program that defines an Action class for each business API entry (entry-point). When your experience is rich, you can also use Dispatchaction to combine these Action classes. You can even define a simple "framework" action to call all of these business classes. You can find the ProcessAction of scaffold design in the Contrib directory, which is a full implementation of a "framework" action. Using this scenario can use less Action class, but you must have a deeper understanding of the bottom level of the Struts and MVC frameworks. Don't be afraid to define too many Actions in the beginning, Struts configuration solutions can give you full freedom to refactor your design, because you can flexibly change your Action class without impacting your application.

Ideally, business logic layers should package all data access details, including database connections. However, some applications design require the caller to get database connections from a DataSource object. When you encounter this situation, the Struts DataSource Manager allows you to configure these DataSource resources when you need it.

The Struts DataSource Manager is defined in the Struts configuration file (Struts-Config.xml). This manager can be used to distribute and configure any database connection pool that implements the Javax.sql.DataSource interface. If your DBMS or container has built-in connecting pools that meet these requirements, you can prefer it.

[JAKARTA public connection pool implementation - BasicDataSource]

If your hand does not connect to the local (Native) implementation, you can use the public connection pool provided by Jakarta [org.apache.commons.dbcp.basicDataSource], which can be very good with the DataSource Manager "Cooperation". In addition, Struts also contains a GenericDataSource class in its UTIL package, which is also a connection pool implementation. But this is just a very simple implementation, not recommended because it may be replaced by BasicDataSource or other data source in Struts. Below is a data source configuration in a struts-config.xml configuration file (implemented using the genericDataSource), you can change the corresponding settings to suit your own system.

The configuration scheme implemented using the BasicDataSource data source is as follows:

Note that you can define more than one data source in your application, you can define multiple data sources as needed and give them logical names. Doing so can give your application to provide better security and scarability, you can define a data source dedicated to test.

After configuring DataSource, you can use these data sources in your application. The following code demonstrates how to generate database connections through these data sources in the Execute method of the Action class.

Public ActionForward Execute (ActionMapping Mapping, ActionMApping Mapping, Actionform Form, HttpservletRequest Request, httpservletResponse response "throws exception {datasource datasource;

Try {datasource = getDataSource (request); cnn = DataSource.getConnection (); // Data connection has been created, you can do what you want to do} catch (sqlexception e) {getServlet (). log ("Processing Database Connection ", e);} finally {// In the finally block contains these code // to ensure that the connection will last closes try {cnn.close ();} catch (sqlexception e) {getServlet (). Log (" Turn off database connections ", e);}}} Note: If you use public BasicDataSource, you provide a query statement (if you set up) must be returned to a line record.

Example: SELECT Count (*) from ValidTable

You can replace ValidTable with any valid table included in your database.

[Use multiple data sources]

If you need to use more than one data source in the module (Module), you can include a Key property in the element of the configuration file.

... attribute configuration, 同 ... ... Property profile, homingly ...

In your code, you can get different data sources through these keys. code show as below:

... try {datasourcea = getDataSource (Request, "A"); DataSourceB = getDataSource (Request, "B"); ...

You can set multiple data sources for each module as needed. But the key attribute of each data source in the same module must be unique, because the Struts module system manages the namespace in each module.

For more details:]

Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24621.htmlhttp://www.mail-archive.com/struts-user@jakarta.apache.org/msg24709.htmlhttp: //www.mail-archive.com/struts-user@jakarta.apache.org/msg24626.htmlhttp://www.mail-archive.com/struts-user@jakarta.apache.org/msg24331.htmlhttp:// Www.mail-archive.com/struts-user@jakarta.apache.org/msg24102.htmlhttp://www.mail-archive.com/struts-user@jakarta.apache.org/msg23501.htmlhttp:///www. Mail-archive.com/struts-user@jakarta.apache.org/msg23455.htmlhttp://www.mail-archive.com/struts-user@jakarta.apache.org/msg23375.htmlhttp://www.mail- Archive.com/struts-user@jakarta.apache.org/msg23321.htmlhttp://www.mail-archive.com/struts-user@jakarta.apache.org/msg23098.htmlhttp://www.mail-archive. Com/struts-user@jakarta.apache.org/msg22713.htmlhttp://www.mail-archive.com/struts-user@jakarta.apache.org/msg21974.htmlhttp: //www.mail-archive.com/ Struts-user@jakarta.apache.org/msg21026.htmlhttp://www.mail-archive.com/struts-user@jakarta.apache.org/msg19 338.htmlhttp://www.mail-archive.com/struts-user@jakarta.apache.org/msg18323.htmlhttp: //www.mail-archive.com/struts-user@jakarta.apache.org/msg14975. Htmlhttp: //www.mail-archive.com/struts-user@jakarta.apache.org/msg14914.htmlhttp: //www.mail-archive.com/struts-user@jakarta.apache.org/msg14435.htmlhttp: //www.mail-archive.com/struts-user@jakarta.apache.org/msg01562.html

Transformation / data transfer http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24480.htmlhtp://www.mail-archive.com/struts-user@ Jakarta.apache.org/msg23623.htmlhttp://www.mail-archive.com/struts-user@jakarta.apache.org/msg10195.htmlhtp://www.mail-archive.com/struts-user@jakarta ..//www.mail-archive.com/struts-user@jakarta. Apache.org/msg10205.html [Dynamic Result Set]

Most database query results will correspond to your already used actionForms, you only need to submit your query results to the corresponding Actionform. But sometimes some fields in the result set are not the properties of the ActionForm, or worse.

Fortunately, Struts's custom label set does not care about the bean type you pass to them. You can even output the result set directly. However, since the results rally keeps the connection with the database, and because they have passed all the data directly to JSP, the page becomes zero. So what should we do?

Starting from Struts 1.1, you can use the newly added ResultSetDynaclass to convert your result set into a Dynabeans ArrayList. The Struts custom label set can use Dynabean properties like a general JavaBean. (For more detailed information on the DynaActionform class, please refer to the Struts User Manual).

The ResultSetdyNaclass class is already included in the beanutils.jar package, so you have to do it now, just take this tool, realize your own delivery plan .........

转载请注明原文地址:https://www.9cbs.com/read-25617.html

New Post(0)