Memento definition: Memento is an object that holds a copy of the internal state of another object, so that the object can later be restored to its previously saved state.
Memento mode is relatively good understanding, we look at the following code:
Copy Code code as follows:
public class Originator {
private int number;
Private file File = null;
Public originator () {}
Create a memento
Public Memento Getmemento () {
Return to New Memento (this);
}
Revert to original value
public void Setmemento (Memento m) {
Number = M.number;
File = M.file;
}
}
Let's take another look at the Memento class:
Copy Code code as follows:
Private class Memento implements java.io.serializable{
private int number;
Private file File = null;
Public Memento (originator o) {
Number = O.number;
File = O.file;
}
}
The values of number and file in originator are saved in the visible memento. You can recover by calling the Setmemento () method by invoking the number and file values in originator.
The disadvantage of memento mode is that it is expensive, if the internal state of a lot, and then save a copy, no intention to waste a lot of memory.
The application of memento mode in Jsp+javabean
In JSP application, we usually have a lot of forms require user input, such as user registration, need to enter names and email, etc., if some of the table items users did not fill out or fill in the error, we hope that the user press "Submit", through the JSP program check, found that there are indeed unfinished projects The red Word displays a warning or error under the item, and also displays the table entry that the user has just entered.
In the following figure, the name is entered by the user, and the word "No" is entered, and we prompt the Red letter warning:
The realization of this technology is to use the JavaBean scope= "request" or scope= "session" characteristics, that is, the memento mode.