Announcement Announcement Module
Collapse
No announcement yet.
right way to use custom editors in non-form velocity pages. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • right way to use custom editors in non-form velocity pages.

    Hi,

    I want to format dates according to the current locale on Velocity pages that are created by a simple controller. Since there is no binding facility, how do I do that?

    Thanks,
    Vincent

  • #2
    Expose a dateTool to your Velocity templates by setting the 'dateToolAttribute' property on either VelocityViewResolver or your individual VelocityView definitions..
    Code:
    <bean 
      id="viewResolver" 
      class="...VelocityViewResolver">
      <property name="dateToolAttribute"><value>dateTool</value></property>
      <!-- other props -->
    </bean>

    then in your VTL do something like;
    Code:
    $dateTool.format&#40;"dd MMM yyyy HH&#58;mm", $someObject.someDate&#41;
    The date tool that you actually get is a Spring version (not the default Velocity version) which takes account of any Locale resolution strategy you happen to be using.

    Regards,

    Comment


    • #3
      Thanks for the insight. The problem is that I don't want the locale to be defined by the system, but by the session/browser locale. There are also other things I want to format/parse, for example currencies and custom objects.

      I generally require the same functionality CustomEditor. The following code is used in a form. This is the kind of functionality I like to use in my non-form controllers, for displaying lists of results and just plain uneditable information.

      Code:
        
         /**
           * Add two custom editors, one to convert a date to the current
           * locale, and one to convert a gender to a Gender object. This
           * method is invoked each time a new control object is created.
           */
          protected void initBinder&#40;HttpServletRequest req,
              ServletRequestDataBinder db&#41; throws Exception &#123;
              
              DateFormat df = DateFormat.getDateInstance&#40;
                  DateFormat.SHORT
                  , req.getLocale&#40;&#41;&#41;;
              
              db.registerCustomEditor&#40;Date.class, new CustomDateEditor&#40;df, true&#41;&#41;;
              
              db.registerCustomEditor&#40;Gender.class, new CustomGenderEditor&#40;true&#41;&#41;;
          &#125;

      Comment


      • #4
        Originally posted by DaVinci79
        Thanks for the insight. The problem is that I don't want the locale to be defined by the system, but by the session/browser locale.
        As mentioned, you can use any locale resolution strategy that Spring supports, and the Velocity tool will take account of this. It will simply work. You can use (for example) an AcceptHeaderLocaleResolver to get locale's specified by the browser.

        Regards,

        Comment

        Working...
        X