Grails SQL date formatting tag lib

Tháng Sáu 17, 2008 at 6:13 sáng Để lại phản hồi

Mình đang tập tọe học Groovy và Grails nên chẳng rành lắm. Khi thử các ví dụ của Grails, một trong những câu hỏi là làm sao customizing layout và formatting của data cho phù hợp với yêu cầu của KH là các cty nước ngòai ỏ Việt Nam (trong nội bộ thì dùng format của Mẽo, nhưng giao diện cho KH trên web và thư từ, báo cáo cho các quan chức thì dùng format VN)? Câu trả lời cuối cùng là customizing Grails tags cho phù hợp. Điều may mắn là tạo tag lib cho Grails rất dễ, nên việc này không hề là “niềm đau chôn dấu” nhưng khi tạo JSP hay JSF tag lib.

Ví dụ: sau đây là tag lib để format date, trích từ http://douglasfils.blogspot.com/2007/09/grails-sql-date-formating-lib.html

Been working with Grails more and created a little tag lib I thought I would post up for others. The date that comes out of the domains is a rather ugly SQL date style so I create a tag lib with the following code:

 def formatSqlDate = { attrs -> 
    def String startdatetime =  "${attrs['targetDate']}"
    def DateFormat odf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
    def DateFormat df = new SimpleDateFormat( "MMM d, ''yy" );
    out << df.format(odf.parse(startdatetime))     
 }

Then I can simple reference it with:

<g:formatsqldate targetdate="${domain.dateObject}">

A person can mode the date format string all they want and also could put in various cases or a flag to select a format style if they wished.

Chi tiết cách tạo và khai báo 1 tag lib trong Grails, mời các bạn đọc trong tài liệu đi kèm với Grails :)

Entry filed under: Groovy. Tags: .

Dùng Groovy để đọc excel file

Gửi phản hồi

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Thay đổi )

Twitter picture

You are commenting using your Twitter account. Log Out / Thay đổi )

Facebook photo

You are commenting using your Facebook account. Log Out / Thay đổi )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


 

Tháng Sáu 2008
T2 T3 T4 T5 T6 T7 CN
« Tháng 3    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

a

Bài viết mới

RSS Jason Rudolph (GRails)

  • Programming Achievements: How to Level Up as a Developer
  • Helpful Resources For Upgrading to Rails 3.1
  • Now Available: Getting Started with Grails, Second Edition
  • Live JavaScript TDD Action Coming to raleigh.rb
  • Git Up! 10 Reasons to Upgrade Your Old Git Installation

Follow

Get every new post delivered to your Inbox.