In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use Hibernate+Struts to achieve J2EE application development", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Hibernate+Struts to achieve J2EE application development" this article.
Data persistence layer and Hibernate
Hibernate is a data persistence layer framework and a tool to realize the mapping between objects and relationships. It encapsulates JDBC with lightweight objects, so that programmers can use object programming ideas to operate databases. It not only provides mapping from Java classes to data tables, but also provides data query and recovery mechanisms. Compared with using JDBC and SQL to operate the database, using Hibernate can greatly improve the efficiency of the implementation. The Hibernate framework defines the mapping relationship between Java objects and data tables in the form of configuration files, and interprets the relationship between data tables as inheritance and inclusion between Java objects at a deeper level. By using HQL statement to describe the complex relational algorithm in the way of object, it simplifies the query of data to a great extent and speeds up the efficiency of development. There is a simple and intuitive API in Hibernate to execute queries against the objects represented by the database. To create or modify these objects, simply interact with them in the program and tell Hibernate to save them. In this way, the business logic that encapsulates a large number of persistence operations no longer needs to write cumbersome JDBC statements, which greatly simplifies the data persistence layer.
Implementation of MVC Architecture with Struts
MVC (Model-View-Controller), proposed by Trygve Reenskaug, was first applied in the SmallTalk-80 environment and is the basis of many interaction and interface systems. According to the requirement of the variability of interface design, MVC divides the composition of the interactive system into three parts: model, view and controller.
The model (Model) is the internal abstraction of the problem logic handled by the software independent of the external display content and form, encapsulating the computational relationship of the core data, logic and function of the problem, independent of the specific interface expression and Icano operation. Views (View) show users information and specific forms that represent model data and logical relationships and states. It obtains display information from the model, and there can be many different display forms or views for the same information. The controller (Controller) handles the interaction between the user and the software, and its duty is to control and provide the propagation of any changes in the model, ensuring the corresponding relationship between the user interface and the model; it accepts the user's input, feeds back the input to the model, and then realizes the computational control of the model, which is the component that makes the model and view work harmoniously. Usually a view corresponds to a controller. With the separation of model, view and controller, a model can have multiple display views. If the user changes the data of the model through the controller of one view, all other views that depend on that data should reflect these changes. Therefore, whenever any data change occurs, the controller notifies all views of the change, resulting in an update of the display. This is actually a change-propagation mechanism of the model.
The Struts framework was first introduced as a part of the Apache Jakarta project. It inherits the characteristics of MVC and makes corresponding changes and extensions according to the characteristics of J2EE. Struts framework is a good combination of Jsp,Java Servlet,Java Bean,Taglib and other technologies. In Struts, it is ActionServlet that plays the role of controller in MVC. ActionServlet is a general control component. This control component provides an entry point for processing all HTTP requests sent to Struts. It intercepts and distributes these requests to the corresponding action classes (which are subclasses of the Action class). In addition, the control component is also responsible for populating Action Form (FromBean) with the corresponding request parameters and passing it to the action class (ActionBean). The action class accesses the core business logic, that is, accessing Java Bean or invoking EJB. The action class transfers control to the subsequent JSP file, which generates the view from the JSP file. All of this control logic is configured using the Struts-config.xml file. In the Struts framework, the view is mainly completed by JSP generated pages, and Struts provides a rich JSP tag library, which helps to separate the presentation logic from the program logic. Models exist in the form of one or more Java Bean. In Struts, there are three main kinds of Bean, which are Action,ActionForm,EJB or Java Bean.
The Struts framework does not specifically define the implementation of the model layer. In actual development, the model layer is usually closely connected with the business logic and has to operate on the underlying data. Here is a development strategy to introduce Hibernate into the model layer of the Struts framework and use it for data encapsulation and mapping to provide persistence support.
Using Hibernate+Struts to develop J2EE Application
1. Architecture
Figure 3 shows the architecture diagram based on the Hibernate+Struts development strategy.
2. Development practice
The following combined with the development practice, take the user login process which is very common in J2EE application development as an example to illustrate how the above architecture is used. The login process is very clear: the user enters the login information from the login page login.jsp, and the system verifies the login information. If it is correct, the login is successful, otherwise it prompts the corresponding error message.
In the process of development, we use Eclipse as the development environment and load the third-party plug-in MyEclipse,Web server that provides better control and support for Struts+Hibernate. Tomcat is used for the server, and Mysql is selected for the database.
First of all, to configure the Hibernate, we only need to modify the hibernate.cfg.xml automatically generated by the system, configure various parameters of the database connection and define the data mapping file. Because the connection pool that comes with Hibernate is mainly used for testing, the performance is not very good, so it can be modified to use Tomcat connection pool through JNDI. The key parts of the profile code are as follows:
< hibernate-configuration > < session-factory >
< property name= "connection.datasource" > java:comp/env/jdbc/ sysdb < / property >
< property name= "dialect" > net.sf.hibernate.dialect.MySQLDialect < / property >
< mapping resource= "User.hbm.xml" / >
< / session-factory > < / hibernate-configuration >
The second is to generate persistence classes. In fact, due to the use of Hibernate, persistence classes and mapping definitions of persistent objects can be generated automatically with the help of wizards in the system. The mapping definition of the persistent object is placed in User.hbm.xml. Some of the codes are as follows:
< class name= "User" table= "tUser" >
< id name= "UserID" column= "User_ID" type= "java.lang.Integer" >
< generator class= "native" > < / id >
< property name= "UserName" column= "User_Name" type= "java.lang.String" >
... < / class >
Thirdly, the Struts framework is used to achieve logic control, and the wizard provided by the system can be used to generate views and controllers. The specific configuration is in the Struts-config.xml file. The related code is as follows:
< form-beans >
< form bean name= "LoginForm" type= "com.LoginForm" >
< / form-beans >
< action-mappings >
< action attribute= "LoginForm" input= "Login.jsp" path= "/ DoLogin"
... .
< / action >
< / action-mappings >
The input page Login.jsp submits the login information filled in by the user to the controller DoLogin for processing, and the controller calls the persistence class User defined by Hibernate to read the corresponding information from the database for verification. The prompt information is displayed in the presentation layer after the operation is completed. The statement User u1=UserDAO.findbyname (username) is used in DoLogin to get the User object, and then the login information can be verified.
From the whole development process, it can be seen that in addition to using the wizard to modify the relevant configuration files, programmers only need to write a small amount of code to complete the coding of the login module. The developed module meets the requirements of the MVC framework, has high quality and is easy to modify and maintain.
The above is all the contents of the article "how to use Hibernate+Struts to achieve J2EE application development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.