user265350
user265350

Reputation:

How to display the current date & time in the Freemarker template section of smooks?

I am able to display the contents of my incoming XML file using smooks in the freemarker template, but I want to add Current Date & time of my local system to identify the execution of my program.

<ftl:freemarker applyOnElement="CreditCard">
    <ftl:template><!--        <BalanceInquiryRequest>
        <TransactionId>${BalanceInquiryRequest.TransactionId}</<TransactionId>
        <ConfigurationId>${BalanceInquiryRequest.ConfigurationId}</ConfigurationId>
        <CardNumberr>${.vars["GiftCard"].CardNumber}</CardNumberr>
        <ExpirationDate>${.vars["GiftCard"].ExpirationDate}</ExpirationDate>
        <SecurityCode>${.vars["GiftCard"].SecurityCode}</SecurityCode>
       *****************************
Here I want to display the current Date & time 
    </BalanceInquiryRequest>
    --></ftl:template>
</ftl:freemarker>

Can you tell me how can I add current date & time in the XML without having an entry in the incoming XML.

Upvotes: 4

Views: 13434

Answers (5)

martbox
martbox

Reputation: 1

You can do it without .now and you don't have to pass in new Date. I'm having to work with old freemarker at the moment and did this instead..

<#assign dateNow = Static["java.util.Calendar"].getInstance().getTime()?datetime />

Upvotes: 0

Ben Taliadoros
Ben Taliadoros

Reputation: 9391

use .now, they introduced it some time ago, no need for java

Upvotes: 8

Daniel Lundmark
Daniel Lundmark

Reputation: 2390

You could write a short groovy script in the Smooks configuration file to populate a bean in the beancontext with today's date. Then the freemarker script could use the value from that bean.

Edit: You can read more about Groovy and Smooks here: http://www.smooks.org/mediawiki/index.php?title=V1.3:Smooks_v1.3_User_Guide#Groovy_Scripting

You probably want to use methods from http://www.milyn.org/javadoc/v1.2/smooks-cartridges/javabean/org/milyn/javabean/repository/BeanRepository.html and do something similar to:

  <g:groovy executeOnElement="xxx">
    <g:script>
    <!--
    addBean("date", new Date());
    -->
    </g:script>
</g:groovy>

You should then be able to access the "date" bean in your freemarker.

Upvotes: 0

BoDiE2003
BoDiE2003

Reputation: 1359

You cannot do it since XML like Freemarker are template engines, not objects. You have to pass it into the java object as new Date();

Upvotes: 0

Quinn DuPont
Quinn DuPont

Reputation: 31

There seems to be an answer here. The short answer, you need to pass in Java.

Upvotes: 3

Related Questions