Archive for Tháng Sáu, 2008
Grails SQL date formatting tag lib
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
Add comment Tháng Sáu 17, 2008