Memento definition: memento is an object that saves a copy of the internal state of another object, so that the object can be restored to its original saved state in the future.
Memento mode is relatively easy to understand, let's look at the following code:
The code copy is as follows:
public class Originator {
private int number;
private File file = null;
public Originator(){}
// Create a Memento
public Memento getMemento(){
return new Memento(this);
}
// Restore to the original value public void setMemento(Memento m){
number = m.number;
file = m.file;
}
}
Let's take a look at the Memento class:
The code copy is 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;
}
}
It can be seen that the values of number and file in Originator are saved in Memento. If the number and file values in Originator are changed, it can be restored by calling the setMemento() method.
The disadvantage of Memento mode is that it consumes a lot. If there is a lot of internal state, save another copy, which will inevitably waste a lot of memory.
Application of Memento mode in Jsp+Javabean
In Jsp applications, we usually have many forms that require users to enter, such as user registration, and need to enter their name and email. If some form items are not filled in or filled in incorrectly, we hope to pass the Jsp program after the user presses "Submit Submit" If you check, you will find that there are indeed items that have not been filled in. A warning or error will be displayed in the red text under the item. At the same time, the table item that the user has just entered is also displayed.
In the figure below, the First Name has been entered by the user, and the Last Name has not been entered, and we will prompt a red warning:
The implementation of this technology is to use the scope="request" or scope="session" characteristics of Javabean, that is, the Memento mode.