Avoid data problems caused by null in velocity

Source: Internet
Author: User

Take a look at the following code:

  1. # Foreach ($ ID in [1 .. 50])
  2. # Set ($User = $ user. Get ($ id ))
  3. $ ID: $ {user. name}
  4. # End
  5. In the above Code, assume that only one user with the ID of 1 exists, but it will show that 50 IDs are the users with the ID of 1 and their names, because in velocity, if the result returned by a value assignment statement is null, the value of the assigned object is not changed, that is, the $ user variable remains the previous valid value.

    The solution provided in the official document is to change the above Code:

  6. # foreach ($ ID in [1 .. 50])
    • # Set ($ User = false)
    • # Set ($ User = $ user. Get ($ id)
    • $ id :$ {user. name}
    • # End

    other methods (recommended):

    the problem was finally met by me. The fundamental solution should be:

    solution: Modify velocity. properties

    original configuration: Directive. set. null. allowed = false

    modified configuration: Directive. set. null. allowed = true

    after the modification is complete, use # If ($ diary) to determine whether the object is empty ~~~

    see: http://www.itsmile.net/blog/article_show/20/273559/Velocity-Set-Null-value-method-Foreach

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.