Today, I will share the process of developing a top 50 project for the Sodi Cup using MyEclipse. This can also be considered as a tutorial on using the open-source SSH framework in Java. Due to my limited familiarity with Spring, I have excluded the Spring part from the entire project. However, I will briefly introduce where Spring can be used. As I am also a beginner, I welcome any corrections or suggestions from experts. Thank you in advance.
First, let's understand the basic environment: Java: J2EE 5 SDK, a package of Java tools provided by SUN; IDE: MyEclipse 7, a well-known Java IDE tool; web: Tomcat 6, a server tool for JSP; DB: MySql5, a free database that requires downloading the corresponding JDBC driver. Framework used: Struts1.2 + Hibernate3
Except for MyEclipse, all these tools are free. You can download them from the internet. I believe you can find a way to use MyEclipse easily. After downloading, install it and configure it as needed. If you are not familiar with configuring the environment, please stay tuned for my next blog post.
Now, let's get started. First, open the MyEclipse environment and create a new Web Project by clicking File->New->Web Project. Enter a name and select the appropriate J2EE version. I recommend using J2EE5, which supports JSTL directly.
Next, enable Hibernate and Struts capabilities for the project. Select the newly created project and click on MyEclipse's Project Capabilities under the Eclipse title, then add Hibernate Capability.
First, select the Hibernate library. I chose Hibernate3 because it is a relatively stable version. Then, select the location to place the Hibernate library. If you are debugging directly on your local machine, you can place it in the build path. If you want to submit or transfer the project, I recommend selecting the second option to copy it to the lib folder under WEB-INF.
Next, select the location of the Hibernate configuration file. Usually, the default location is fine, so just click Next.
Then, select the database connection configuration. If you have already configured the database connection information, simply select it. In my case, I used my own configured MySql.
Next, select the location of the Hibernate session factory. You can place it in the package under the hibernate folder in the source code for easy refactoring. You can choose an existing location or create a new one. Finally, click Finish to complete the Hibernate support configuration.
Next, we will configure Struts support. The steps are similar to configuring Hibernate. Go to Project Capabilities -> Add Struts Capability, then select the appropriate Struts version (I chose 1.2). Configure the package path and click Finish. If you need Spring support, the steps are similar. I will skip that part here.
With the configuration of Struts + Hibernate complete, it's time to work on the Hibernate data objects. MyEclipse provides a convenient method for this. First, open the DB browser and connect to your configured database. If you haven't configured one, you can create a new connection by providing the driver and connection string. You can find the DB browser under Window -> Show View. Select the tables you want to generate.
Right-click and select Hibernate Reverse Engineering. This option will open a wizard.
Configure the source path and package name for your project. Select the option to create DAOs. If your table constraints are correct, simply click Finish. This will generate the corresponding DAO files in the source folder.
Next, we will configure the detailed settings for Struts to implement the presentation layer. MyEclipse automatically integrates the FreeMarker plugin, so we can use it directly. Open the Struts-config.XML file under WEB-INF and enter the design interface. You can create Actions, FormBeans, JSPs, etc., according to your needs. Below is the final result for CG.
There are a total of four JSP pages. The "editor" page is a JSP with a form. There is one Action class, GlobalAction, which handles all the actions. There is also a global forward for error handling.
Now, it's time to design the front-end. Design your own pages. You can find the source code for this tutorial in CG's blog. I won't go into detail here.
Next, create JavaBean objects for the business logic layer and Service classes for the related tables. These Service classes will perform operations such as reading and modifying data from the DAO objects of the lower layer. Combine these operations to form a complete table service class that can be called by the Action in the upper layer, separating the Action from the DAO. In addition to creating individual service classes for each table, CG also created a SpanTableService class for cross-table queries. I recommend using the interface and implementation class approach, as it allows for easy implementation of multiple table service classes without the need for separate implementations. The current file list is as follows:
Below is a screenshot of the main implementation class, Student. The other tables are similar.
Great! The business logic layer is almost complete. Please note that CG created a static class, DAOProvider, to facilitate the creation of DAO objects. It is located in the util package. Additionally, to provide Service objects in the Action of the presentation layer, CG created a ServiceProvider class. If you are using Spring, you can consider using Spring to manage the corresponding objects and automatically generate them. Since we don't have Spring here, we can only use the Provider for now. For this simple demo, directly returning the objects is sufficient. You can consider using XML configuration for the DAO list. I won't go into detail here. Below is a screenshot of the specific code.
Now, let's move on to the most critical class, GlobalAction. This class handles user requests and processing. CG uses the Dispatch forwarding class, which processes different actions based on the parameters in the request. Simply handle the corresponding action request and select the appropriate forwarding after processing. Below is a screenshot of the code for returning a list of students.
Great! The business logic layer is basically complete. Now, let's go back to the presentation layer. Here, we need to handle the dynamically generated data. If you are using Struts, you will need to use Struts tags. Here is a code snippet for iterating through all the properties of the stu Bean and outputting them.
If you have completed all of these steps, don't rush to debug yet. There are a few more things to configure. Configure an EncoderFilter, which you may have used if you have learned JSP. I will explain it briefly. CG wasted some time due to a configuration mistake. You can find the source code for the EncoderFilter class in the Util package. Below is a screenshot of the code. Make sure it extends HttpServlet and implements Filter. Don't make a mistake like CG did.
Then, configure this Filter in web.xml under WEB-INF. Here is a screenshot.
Finally, if you are using EL expressions, here's a tip for you. You can directly configure it in web.xml, as shown in the screenshot above. This way, you don't need to include <%@ page isELIgnored="false" %> in each page.
Now, you can start debugging. Start Tomcat and deploy your project. CG's Sodi task is complete. I have shared the development process with you. If you have any questions, feel free to leave a comment. You can download the source code from the following link:
For the running effect, please refer to CG's recent blog posts. Your comments and suggestions are welcome. Thank you.
Original blog post by Li Daren, please indicate the source when reposting from Li Daren's blog.