Results 1 to 4 of 4

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

  1. #1
    Join Date
    Aug 2004
    Posts
    12

    Default 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. #2
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    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,
    Darren Davison.
    Public Key: 0xE855B3EA

  3. #3
    Join Date
    Aug 2004
    Posts
    12

    Default

    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;

  4. #4
    Join Date
    Aug 2004
    Location
    London, UK
    Posts
    339

    Default

    Quote 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,
    Darren Davison.
    Public Key: 0xE855B3EA

Similar Threads

  1. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  2. Replies: 2
    Last Post: Aug 2nd, 2006, 10:18 PM
  3. displaytag in Wizard Form pages
    By gaccx2 in forum Web
    Replies: 1
    Last Post: Jun 14th, 2005, 04:41 AM
  4. FreeMarker vs Velocity
    By Martin Kersten in forum Architecture
    Replies: 8
    Last Post: May 30th, 2005, 09:21 AM
  5. Replies: 0
    Last Post: Oct 7th, 2004, 09:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •