Beginner easy to get started with SSH-struts2 03 Data encapsulation, easy to get started with ssh-struts2
In this chapter, we can obtain data to see what is different from the previous chapter. There are also three data encapsulation methods. Next we will introduce them one by one.
First: attribute encapsulation.
Class. There are two attributes: name and pwd. Enter getter and setter for these two attributes.
The page uses the login. jsp in the previous chapter. You only need to change the submitted address and configure struts. xml to get the data. Configuration has been introduced before the first two chapters.
The second type: Model driver: The action class implements the ModelDriven interface and the getModel method of the ModelDriven interface.
Privatize an object. This object is returned in the getModel method.
Last: expression Encapsulation
01 declare the object class in the Action class (note that only the object class is declared ),
02: generate the set and get methods for object names of object classes,
03: Modify the attribute value of a form:
Name = "Object name. property name" (users. username)
There are two encapsulation methods after the three methods, which are encapsulated into the List and Map sets.
1. List Set
Page:
Action:
Map set:
Page:
Action:
Note: In an action, attribute encapsulation and model encapsulation cannot be used for the same form. If an action is used, only model driver encapsulation is executed.
Similarities and differences between expression encapsulation and model encapsulation:
Similarities:
Data can be encapsulated into Entity classes.
Differences:
Model encapsulation can only encapsulate data into one entity class, and only one ModelDriven interface can be implemented once in one action. Therefore, model encapsulation can only encapsulate one object class.
Expression encapsulation can encapsulate data into multiple objects.
I have not introduced much about Data encapsulation.