The requirement is that after the form is submitted, go to another existing page and display a conspicuous message in the blank area at the top of the page, and only display once (this message should not appear again after refresh ).
Very unfamiliar with web development, pair said this requirement is similar to the Flash message in ror and asked if there is any in Java. we used springmvc + velocity to read the document and asked the person who was asked about the solution.
Let's give it a try. I knew it was almost impossible to succeed, but I wrote the following:Code:
- Model. Put ("Flashmessage","I'm stupid");
- Return NewModelandview (NewRedirectview ("Anotherpage"), Model );
Sure enough. The model cannot span the controller. Is it in the session? With my limited Web experience, I should be able to solve the problem: When should I clear it from the session?
This should be a concept: You put something into the session, but you can only get it once, and it will not exist next time.
Class is used to represent the concept. We only need to add ourselves to the session when generating the object, and remove ourselves from the session in the provided function, so you will not get it for the second time.
-
- ImportJavax. servlet. http. httpsession;
-
-
-
- Public ClassThrowawayobject {
-
- PrivateHttpsession session;
-
- PrivateString attribute;
-
- PrivateObject value;
-
-
- PublicThrowawayobject (httpsession session, string attribute, object Value ){
-
- This. Session = session;
-
- This. Attribute = attribute;
-
- This. Value = value;
-
- Session. setattribute (attribute,This);
-
- }
-
-
-
- PublicObject getvalue (){
-
- Session. removeattribute (attribute );
-
- ReturnValue;
-
- }
-
- }
In the controller, you only need:
- NewThrowawayobject (Session,"Flashmessage","I'm stupid");
In view, here is the velocity template:
- # If ($ flashmessage)
- <Script language ="JavaScript">
- Shownotificationmsg ("$ Flashmessage. Value")
- </SCRIPT>
- # End
So what are the standard practices for implementing such functions in spring MVC? What about other Java Web frameworks?