Combined with Struts and Hibernate to talk about data representation of J2EE architecture
Author: PO robbin In this structure struts hibernate, Hibernate should not be generated is transmitted directly to the JSP, whether he is Iterator, or List, which is a design error. I will talk about the data representation of each layer in the J2EE architecture: the data representation of the web layer is FormBean, data from the HTML Form Post business layer is indicated by the VO persistence layer, and its data is derived from the database. The data of the persistence layer represents, for example, CMP in a specified J2EE architecture, data representation of different layers should be limited to the layer, and should not spread to other layers, which can reduce the coupling between the layers, improve the integral of the J2EE architecture Maintenance and scalability. For example, the logic of the Web layer has been modified, then only the structure of the FORMBEAN does not need to touch the code modification of the business layer and the persistent layer. Similarly, when the database table has been adjusted, only the persistence layer data is required without the need to touch the business layer code and the web layer code. However, due to the powerful function of Hibernate, such as dynamically generating PO, PO's state management can be detached from Session so that the PO is fully acting as Vo, so we will consolidate the PO and VO, which is collectively referred to as PO. Let me talk about the significant difference between ActionFormBean and PO of the persistent layer. In a simple application, ActionformBean and Po are almost no different, so many people simply act as PO, so ActionFormBean will go to the service layer from the JSP page to the servlet control layer, then pass through the persistence layer, and finally mapped to the database table. It's really a bit! But in complex applications, ActionFormBean and PO are separated, they are not possible. ActionformBean is a corresponding one of the Form form inside the web, what is the element in the form, what is the property in Bean. And the PO and database table correspond, so if the database table does not modify, the PO will not modify. If the page process and database table field correspondence, how can you use ACTIONFORMBEAN to replace PO? For example, the user registration page requires the basic information of the registered user, so the HTML Form contains the basic information properties, so you need an actionformbean to correspond to (Note: Yes, each correspondence), each bean property corresponds to a text box Or choose the box. And what is the user's lasting object? What is his attributes and what is the significant difference between Actionformbean? He will have some collection attributes that are not available, such as users' permissions attributes, user group properties, user posts, etc. It is also possible that there are three attributes in ActionFormBean, namely the user's first name, Middle Name, Last Name, and a Name object property in my user's persistent object. Suppose my registration page is originally as long as you provide first name, then ActionFormbean later, I will provide you with the full name, you have to change ActionFormbean, add two properties. But this time PO should not be modified, because the database has not been changed.
So how should I make a reasonable design in a complete J2EE system? JSP (View) ---> ActionformBean (Module) ---> Action (Control) ActionformBean is a data representation of the web layer, which corresponds to the HTML page FORM, as long as the WEB page changes, it should be carried out Modify, it should not be passed to the business layer and persistence layer, otherwise, once the page is modified, it will always be implicated to the large-area code of the business layer and the persistence layer for modification, for the maintenanceability and scalability of the software. It is a disaster, ActionT is his border, so far! Action (Web Control) ---> Business bean ---> DAO ---> ORM ---> DB and PO is a data representation of the business layer and persistence layer, which flows between business layers and persistence layers. He should not be passed to the web layer of View, and ActionServlet is his border, so far! Then take a look at the entire architecture: When the user accesses the web, a page is submitted. So Action got this formbean, he will read the FORMBEAN property, then construct a PO object, then call the BEAN class of the business layer, complete the registration operation, redirect to the success page. After the business layer bean receives this PO object, the DAO interface method is called for persistent objects. When the user queries a member's information, he queries with a full name, so Action gets a UserNameFormBean includes 3 properties, respectively, first name, middle name, last name, then Action reads the 3 attributes of UserNameFormbean. , Construct the NAME object, then call the business bean, pass the Name object to the business bean, and query. Business bean gets Name (Note: Name Object is just a property of the User) After calling the DAO interface, returns a USER's PO object, note that this user is different from the UserformBean used in the web layer, he has a lot of collection attribute drops. The business bean then returns the User object to the action. After the Action gets User, remove the basic property of the user (the collection attribute if you don't need it), construct userformbean, then put userformbean request.settribute (...), then redirect to the Query Results page. The query page gets the ActionFormBean in the Request object, and the TAG is automatically called. Summary: FormBean is a data representation of the web layer, he cannot be passed to the business layer; PO is the data representation of the persistence layer, in a particular case, such as Hibernate, he can replace VO appear in the business layer, but regardless of PO or VO It must be limited to use in the business layer, and the Control that reaches the Web layer is never spread to the view. Data conversion between FormBeans and POs is to drip in Action. BTW: JDO1.x is still not as strong as hibernate functions, and PO cannot be separated from the persistence layer, so it must be used in the business layer, so a large number of VO and POs must be performed in the business layer, and the transformation comparison is compared to Hibernate. Cumbersome.