Spring Web Flow – displaying your JSR-303 validation messages using FreeMarker

Instead of creating a custom validator in the traditional sense I’m relying on JSR-303 annotations to specify validation constraints, because, it is the future, and I shouldn’t have to do a load of coding to specify common validation requirements like an email address regular expression or a numeric value that is required to be within a defined range.

Using Spring web flow, I found that binding error messages weren’t available when I called “<@spring.showErrors ‘<br/>’/>” in my freemarker view (after I had bound the field I wanted to show errors for of course).

So where are my error messages?

It turns out that Spring Web Flow has a different way of providing the user with feedback messages. The Spring Web Flow reference documentation says: “Spring Web Flow’s MessageContext is an API for recording messages during the course of flow executions”.

The message context (with all your binding error messages in it) can be found here: flowRequestContext.messageContext. I’ve written a couple of macros to make retrieving error messages from this object a little easier:

<#--
 * Shows flow messages (which reside in flowRequestContext.messageContext)
 *
 * @param source Name of the field that caused the error
 * @param severity String representation of org.springframework.binding.message.Severity
 * @param separator the html tag or other character list that should be used to
 *    separate each option. Typically '<br>'.
 * @param classOrStyle either the name of a CSS class element (which is defined in
 *    the template or an external CSS file) or an inline style. If the value passed in here
 *    contains a colon (:) then a 'style=' attribute will be used, else a 'class=' attribute
 *    will be used.
 * @param tag The HTML tag to wrap the error in
-->
<#macro showFlowMessages source severity separator classOrStyle="" tag="">
  <#assign messages = flowRequestContext.messageContext.getMessagesBySource(source)/>
  <#if (messages?size > 0)>
    <#list messages as message>
      <#if message.severity?string == severity>
        <#if classOrStyle == "" && tag == "">
          ${message.getText()}
        <#else>
          <#if classOrStyle == "">
            <${tag}>${message.getText()}</${tag}>
          <#else>
            <#if tag == ""><#local tag = "span" /></#if>
            <#if classOrStyle?index_of(":") == -1><#local attr="class"><#else><#local attr="style"></#if>
            <${tag} ${attr}="${classOrStyle}">${message.getText()}</${tag}>
          </#if>
        </#if>
        <#if message_has_next>${separator}</#if>
      </#if>
    </#list>
  </#if>
</#macro>

<#--
 * Shows flow messages (which reside in flowRequestContext.messageContext) in an ordered or unordered list
 *
 * @param source Name of the field that caused the error
 * @param severity String representation of org.springframework.binding.message.Severity
 * @param classOrStyle either the name of a CSS class element (which is defined in
 *    the template or an external CSS file) or an inline style. If the value passed in here
 *    contains a colon (:) then a 'style=' attribute will be used, else a 'class=' attribute
 *    will be used.
 * @param ordered Whether or not the macro should output the list as an <ol> or <ul>
-->
<#macro showFlowMessagesList source severity classOrStyle="" ordered=false>
  <#local errorsList><@showFlowMessages source, severity, "", "", "li" /></#local>
  <#if errorsList?trim != "">
    <#if classOrStyle == "">
      <#local attr="">
    <#elseif classOrStyle?index_of(":") == -1>
      <#local attr=" class=" + classOrStyle>
    <#else>
      <#local attr=" style=" + classOrStyle>
    </#if>
    <#if ordered><ol${attr}><#else><ul${attr}></#if>
    ${errorsList}
    <#if ordered></ol><#else></ul></#if>
  </#if>
</#macro>

I’ve added these to my spring extensions, which you can download here: springx.ftl

1,837 thoughts on “Spring Web Flow – displaying your JSR-303 validation messages using FreeMarker

  1. Delve into mysteries of hidden surprises and bonuses. In ignation casino enigmatic boxes await. Unravel and reap

  2. Revel prevail plays equitable and entertainment odds balanced harmonies in. In ignation boom and balance. Revel and rise

  3. Illuminate jackpot glows and bonus evenings. In ignation casino log in radiant rewards gleam. Illuminate and indulge

  4. Journey through exciting quests and bonus rounds for extra prizes. In ignation casino multilingual support caters to players worldwide. Start your global gaming quest today

  5. Play at the premier world of online gaming where endless fun awaits. Bovada USA offers top baccarat and generous welcome bonuses for all players. With Bovada enjoy fantastic wins and secure reliable entertainment every day

  6. Explore the electrifying world of online gaming where endless fun awaits. Bovada Current Promo offers top table games and cashback offers for all players. With Bovada enjoy premier wins and secure reliable entertainment every day

  7. Hook epic wins in Big Bass Bonanza Money symbols wild collection = bonanza-level excitement every spin.

  8. Big Bass Bonanza gameplay explodes with potential Collect cash values and watch your balance grow.

  9. Gates of Olympus is what happens when mythology meets math. Tumble engine random Zeus bombs = explosive entertainment. Are you brave enough for gates of olympus big win?

  10. One name echoes through every online casino forum — zeus55. Zeus-powered cascades and sky-high multipliers made it immortal. Play Gates of Olympus — become part of the legend.

  11. sugar rush multiplier spots — dangerously sweet Tumbling symbols growing multipliers and huge payout potential. Dare to spin?

  12. Prepare your sweet tooth sugarrushslotx delivers non-stop cascades wild multipliers and massive bonus rewards. Spin for a sugar-coated fortune

  13. Get your glow on with starslot55. Expanding wild feature turns good spins into great ones. Both-ways wins high RTP pure magic.

  14. starburst free spins no deposit lights up any casino lobby. Cosmic gems shine bright as wilds expand and trigger thrilling respins. Perfect for casual spins or big sessions — join the starburst big win revolution now.

  15. luckyland slots free coins: Your ticket to endless excitement Grab 7777 Gold Coins 10 Sweeps Coins on signup today. Play magical games and win redeemable cash

  16. The community is huge. The wins are bigger. Join stake sportsbook and become part of the legend.

  17. Unlock jackpot potential with luckyland slots real prizes Claim your bonus: 7777 Gold Coins and 10 Sweeps Coins instantly—no promo code. Play free and win real prizes

  18. Don’t miss your chance to win big at betmgm ontario casino. Get 100 deposit match up to 1000 plus 25 free play right now. The most exciting casino games are here.

  19. Join DraftKings casino login Casino and level up your game. Wager 5 to unlock 500 bonus spins on Cash Eruption with up to 1K credits covering first-day net losses. The action never stops

  20. Take the throne at kings maxxwins Casino. Wager 5 for 500 bonus spins plus up to 1K back in your opening 24 hours. Massive games massive potential

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>