A Web application was created using Eclipse Web Tools Project, the Tomcat application server, and the MySQL database server. Although the application (dbtest) may be good, there are some limitations: in the servlet code, the Java Server Pages (JSP) name is hard-coded in SQL and is hard-coded into the command class.
Fortunately, these problems can be solved by two interesting solutions. The first problem can be solved by using the open source Struts framework, which separates the application's models, views, and controllers by mapping the model actions to view components (such as JSPs) in a simple configuration file.
The second problem can be solved by using a framework that provides Java and relational database persistence. The Hibernate framework provides a powerful, high-performance mapping engine between objects and database tables. This article will use the following techniques:
J2SE 5.0 jre:http://java.sun.com/j2se Eclipse 3.1:www.eclipse.org WTP 1.0:www.eclipse.org/webtools Tomcat 5.0:http:// Jakarta.apache.org/tomcat/mysql 4.0.25:www.mysql.com MySQL connector/j driver 3.1:www.mysql.com/products/connector/ J/struts 1.1:http://struts.apache.org Hibernate 3...www.hibernate.org Application Overview
Let's recap what we did last time. The basic Web application implements the following use cases:
Customers must register at the site the following single customer can place a single customer can browse their own order admin can list all registered customers
The system is implemented using the Universal SERVLET/JSP programming model, the MySQL database and the Tomcat application server. The system domain model is represented by customer (customer) and order (orders) two classes (see Figure 1).
Two corresponding database tables customers and orders were created to represent the data held by these objects. 4 Database Command classes responsible for executing the above use cases, as well as four servlet as controllers, were created to collect user input information, invoke these commands, and forward responses to the appropriate JSP. The Commandexecutor class is responsible for processing database connections using the Tomcat connection pool.
Add Struts Support
Import the Dbtestwar file into the Eclipse workspace by using the File-import option and selecting the War file to import. If there is no dbtest item in the workspace, the above action is OK. If you already have a dbtest item in your workspace, right-click on an existing item in the Navigator view, and then select Copy and paste to save the current item. When prompted for a new project name, select Dbteststruts as the new project name so that you do not overwrite the existing project. Now, to add struts support, you must copy the following files to the Web-inf\lib folder: Struts.jar, Commons-lang.jar, Commons-collections.jar, Commons-beanutils.jar, Commons-validator.jar, Commons-logging.jar, Commons-digester.jar, Commons-fileupload.jar.
All of these files are available for download from the Struts Web site, which contains the struts framework and the corresponding Apache Commons package, which handles such things as internationalization, collection operations, utilities, validation, logging, Digester, as well as file upload operations and other features required. All of the above are components supported by struts. This article does not use all of the above features, but struts relies on many of these features, for example, when parsing the struts configuration file, the Digester feature is used heavily. These features are useful when you need to use services such as logging, file uploads, and so on.
Therefore, to add the following files to the Web-inf folder: Struts-config.xml, Struts-bean.tld, Struts-html.tld, Struts-logic.tld, Struts-nested.tld, Struts-template.tld, Struts-tiles.tld.
The most important of these is the Struts-config.xml file, which is the main configuration file for the Struts framework and contains definitions of all action mappings, data sources, plug-ins, and so on. See the example in Listing 1.
Manifest 1:struts configuration file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- Data Sources -->
<data-sources>
</data-sources>
<!-- Form Beans -->
<form-beans>
</form-beans>
<!-- Global Exceptions -->
<global-exceptions>
</global-exceptions>
<!-- Global Forwards -->
<global-forwards>
</global-forwards>
<!-- Action Mappings -->
<action-mappings>
</action-mappings>
</struts-config>