Crud of struts
1 What is crud: crud represents the CREATE (ADD), read (read), update (update), and delete (delete) of a framework)
2. How can this problem be solved ??
In fact, the crud of struts2 is the same as the actual method, but it is more convenient to set and obtain attributes. Because of the value stack, with the ognl object expression, therefore, struts2 crud is more convenient.
I don't need to talk about it anymore. Let's analyze it now !! (Data is used to connect to the database)
Create ------> Add: add data and save the data to the database
It must be to get a saved object, but the submitted action class and object class are not the same ognl match, not my action class. How can I get the submitted object? The idea of inertia must be taken from the value stack, but it is troublesome to take it out from the value stack and assign values one by one, or write the Set Method () with the same attribute in the action class, therefore, struts2 provides a simpler method to implement the modeldriven interface ---------- which is used to push the required objects to the top of the stack.
Formal process:
1. submit data in a form
2. Implement the modeldriven interface in the action class, implement the GetModel () method, and push the desired object to the top of the stack, that is, return to the object
3. The function of the GetModel method is to push the object to the top of the value stack.
4. Then, the paparmeterinterceptor Interceptor (which will call this interceptor by default) matches the parameters in the request with the value stack object one by one, in this way, our objects are taken from the top of the stack, and then put into the database according to the corresponding method.
The Code is as follows:
Form submission data:
<s:form action="emp-save"><s:textfield name="firstName" label="FirstName"></s:textfield><s:textfield name="lastName" label="LastName"></s:textfield><s:textfield name="email" label="Email"></s:textfield><s:submit></s:submit></s:form>
Action Processing request:
Public class employeeaction implements modeldriven <employee> {
Private employee;
Public String save (){
Dao. Save (employee );
Return "success ";
}
Public Employee GetModel (){
This. Employee = new employee ()
Return employee;
}
}
Data class ---- simulated Database
Public class Dao {Private Static Map <integer, employee> EMPs = new linkedhashmap <integer, employee> (); static {// simulate the database EMPs. put (1001, new employee (1001, "AA", "AA", "[email protected]"); EMPs. put (1002, new employee (1002, "BB", "BB", "[email protected]"); EMPs. put (1003, new employee (1003, "cc", "cc", "[email protected]"); EMPs. put (1004, new employee (1004, "DD", "DD", "[email protected]"); EMPs. put (1005, new employee (1005, "ee", "ee", "[email protected]");} /** used to traverse **/public list <employee> getemployees () {return New arraylist <employee> (EMPs. values ();}/** used to delete **/Public void Delete (integer empid) {EMPs. remove (empid);}/** used to save **/Public void save (employee EMP) {long time = system. currenttimemillis (); // EMP used to generate the ID. setemployeeid (INT) time);/** get data by ID **/public employee get (integer empid) {return EMPs. get (empid );}
Process Analysis:
Form submission: Send the Save method of the requested action. Everyone knows the process of the action request. After the action executes the Save method, it will continuously call back various interceptors, there is a modeldriven. The Interceptor is used to push the desired objects to the top of the stack, because the paparmeterinterceptor interceptor is called by default ,), the Interceptor is to match the parameters in the request with the value stack object one by one, so that the attributes of our objects will be matched one by one, and we will take them from the top of the stack, then put the data in the database according to the Save method of Dao. The whole process is like this.
Form submission ----> action method of the action class ------> because the modeldriven interface is implemented (I will analyze the source code next time) -----> the GetModel () method is called, this method is used to push the required object to the top of the stack --> paparmeterinterceptor Interceptor to match ------> the data submitted by the form is matched to our object, then we can put the data in the database according to the corresponding method.
Struts2 CRUD operation