content:
I. The meaning and defect of the Actionform class in Struts.
There is also a Java area:
Teaching tools and product code and components all articles practical skills
Xingbo Tao (xbt@bhsky.com) November 2003
This article tells how to use Java reflection mechanism to simplify the development of Structs applications.
I. Introducing the Actionform class in Struts: In the Struts application, ActionForm is a very important concept that provides data that maps the client form to the Action operation (if the customer specified in the customer) The data is also included to check the data). Action modifies the data status based on the needs of business logic. After changing the system state, ActionForm automatically retracts new data status and keeps. Programmers correspond to the correspondence between JSP and Actionform Bean, and JSP and ACTIONFORM are 1: 1, or N: 1, in this, Struts itself does not propose its own point of view. Whether it is one-on-one, still more, Struts itself doesn't care, it can work very well. Struts pointed out in its development documentation for smaller scale development, developers can write only an actionform bean according to their own needs, and even the entire application only writes an actionform bean. Of course, Struts is not against each Actionform bean only corresponds to a JSP, and the correspondence between them is determined by the developers. In my opinion, the Entity EJB is the same as the significant contribution of J2EE. Entity EJB makes the programmer's access to the two-dimensional relational database, and the programmer can use the SET or GET or the like to manipulate the data of the relational database. And ActionForm also enables programmers to have a miracle-like object of data access, and programmers can also use SET or GET or other object-oriented methods to access data on the web page, which is a major development mode change. Based on this, I personally think that the relationship between Actionform and JSP is the relationship between the VIEW layer, so that it will be clearer in understanding. However, this will also bring a very real problem, in an application, may have a lot of JSP pages, if each ActionForm can only correspond to a JSP page, then the system's Java code will drastically expand, and Each ActionForm is only very simple SET or GET method access data, then how to simplify the development of Struts applications? In Struts1.1, Struts introduced DynaActionform and Dyna Bean, trying to solve this problem, in my opinion, the introduction of DynaActionform, destroying the concept of web page access objectivity, so that the developers returned to use HashTable, Map, Collection, ArrayList, etc., etc., to achieve the old road to access the data. Although the flexibility of the application has increased, the readability of code is also greatly reduced, and the difficulty between developers has also increased. In the traditional application of the Actionform Bean, we usually write as follows:
Connection conn = drivermanager.getConnection ("JDBC URL");
SQL = "SELECT * from home Tables"; preparedStatement Stmt = conn.preparestatement (SQL);
ResultSet RS = stmt.executeQuery ();
Arraylist array = new arraylist ();
While (rs.next ()) {
AACTIONFORM ACTIONFORM = New AActionform ();
Actionform.SetId (rs.getstring ("id"));
Actionform.SetName (Rs.getstring ("name");
Array.Add (Actionform);
}
In an Action's Execute method, we store this collection with Request.SetAttribute ("array", array), and then in the JSP page, we use the Iterate TAG to generate the data loop. The code is usually this look:
Type = "com.bhsky.webis.goods"> td> td> TR> logic: Iterate> logic: present> In Struts, the access and display of the data is usually very fixed. In the View layer, we have no way to simplify your code, in the Action layer, its write is usually very fixed, just do a page jump Turning, business logic and access to data are usually placed in JavaBean. Then, here, I propose a mechanism to use class reflection, enabling the application to assign value to the ActionForm Bean, that is, the application can be assigned to the ActionForm bean by using a simple interface, using a universal interface. You don't have to manually assign a value to the Actionform Bean in each place where ActionformBean is used, and then it is displayed in the JSP page. Although it does not reduce the number of Actionform Beans, it is at least to automate the application of ActionForm Bean, thereby reducing programmatic probability, and improving the development efficiency of Cheng Software. Second, the concept of class reflection: About the concept of reflection, I will not introduce it in detail here. It is not the focus of this article. There are a lot of articles on the IBM DeveloperWorks website. You can find it. In fact, Struts itself uses a large number of reflex mechanisms. Third, how to apply a class reflex mechanism to simplify the development of the Struts application: 1. First define action form .BHSky.Webis.System; Import org.apache.struts.action. *; Import javax.servlet.http. *; Public class userSActionform extends actionform { PRIVATE STRING USR_ID; PRIVATE STRING USR_NAME; Public void setusr_id (string usr_id) { THIS.USR_ID = USR_ID; } Public String getusr_id () { Return usr_id; } Public String getusr_memo () { Return USR_MEMO; } Public void setusr_name (string usr_name) { THIS.USR_NAME = USR_NAME; } } 2. Write generic methods for assigning ActionFormBean: / // function: Complete the RESULTSET object to the transformation of an object to the arraylist object // Para: SQL, specified query SQL // Para: classname, SQL relative to the name of the JavaBean / Formbean class // Return: With the result set of ClassName as a record, complete the conversion of the RESULTSET object to the arraylist object as the set // combined ClassName object // Public ArrayList Select (String Sql, String ClassName) { ArrayList paralist = new arraylist (); Try { IF (conn == null) { CONNECTION (); } PreparedStatement Stmt = conn.preparestatement (SQL); ResultSet RS = stmt.executeQuery (); String RecordValue = "" Object C1 = NULL; Paralist = new arraylist (); ResultSetMetadata RSMD = rs.getMetadata (); int columncount = rsmd.getColumncount (); While (rs.next ()) { C1 = Class.Forname (classname) .newinstance (); For (int i = 1; i <= columncount; i ) { IF (rsmd.getcolumnname (i))! = null) { RECORDVALUE = rs.getstring (rsmd.getcolumnname (i)); } else { RecordValue = ""; } Method M = c1.getClass (). getMethod (RSMD.GetColumnname (i)), New class [] {recordvalue.getclass ()}); M.INVOKE (C1, New Object [] {RecordValue}); } Paralist.Add (C1); } } catch (sqlexception ex) { } catch (classnotfoundexception e) { } catch (nosuchmethodexception e) { } catch (invocationTargeTexception E) { } catch (IllegaCcessException E) { } catch (instantiationException e) { } finary { CloseConnection (); Return Paralist; } } 3. Call the SELECT method in the business logic of the JavaBean package, then display it on the JSP page: // Function: Get a list of users // Para: // Return: Return to the user list / Public arraylist getusers () { ArrayList Ret = NULL; DatabaseManage DB = New DatabaseManage (); String SQL = "SELECT USR_ID, USR_NAME" "from users"; Ret = db.select (SQL, "com.bhsky. Webis.system.usersActionform"); Return Ret; } 4. Call the getUsers () method in the action method: Public ActionForward Execute (ActionMApping ActionMApping, Actionform Actionform, HTTPSERVLETREQUEST REQUEST, HTTPSERVLETRESPONSE HTTPSERVLETRESPONSE) { / ** @ Todo: Complete The Business Logic Here, This Is Just A Skellon. * / UserSactionForm UAF = (userSactionform) Actionform; SystemService Ubb = new systemservice (); ArrayList UserList = ubb.getusers (); Request.setattribute ("UserList", UserList); ActionForward ActionForward = ActionMapping.FindForward (URL); Return ActionForward; } 5, displayed in JSP: