Throwawayobject: implement the Flash message in rails in Java

Source: Internet
Author: User

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:

    1. Model. Put ("Flashmessage","I'm stupid");
    2. 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.

  1. ImportJavax. servlet. http. httpsession;

  2. Public ClassThrowawayobject {
  3. PrivateHttpsession session;
  4. PrivateString attribute;
  5. PrivateObject value;

  6. PublicThrowawayobject (httpsession session, string attribute, object Value ){
  7. This. Session = session;
  8. This. Attribute = attribute;
  9. This. Value = value;
  10. Session. setattribute (attribute,This);
  11. }

  12. PublicObject getvalue (){
  13. Session. removeattribute (attribute );
  14. ReturnValue;
  15. }
  16. }

In the controller, you only need:

    1. NewThrowawayobject (Session,"Flashmessage","I'm stupid");

In view, here is the velocity template:

    1. # If ($ flashmessage)
    2. <Script language ="JavaScript">
    3. Shownotificationmsg ("$ Flashmessage. Value")
    4. </SCRIPT>
    5. # End

So what are the standard practices for implementing such functions in spring MVC? What about other Java Web frameworks?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.