Using FreeMarker, I want to display a date into milliseconds:
${mydate?long}
but I get as output a comma separated millisecond:
524,354,400,000
is there any built-in function in Freemarker to remove comma ?
Thanks
It looks like as of version 2.3.17 you can use:
${myDate?long?c}
http://sourceforge.net/p/freemarker/feature-requests/72/
As an alternative you could write on your Freemarker template this directive:
<#setting number_format="computer">
This will remove all commas from numbers.
This works fine for Freemarker 2.3.23
More info about these directives can be found here:
http://freemarker.org/docs/ref_directive_setting.html
Adding to Gil's answer, if you build the configuration inside your code, you can set the flag globally by :
Configuration cfg = new Configuration();
...
cfg.setNumberFormat("computer");
Copied from the comment of the accepted answer,
In my version (2.3.26), simply ${myDate?c} will suffice, assuming that
myDate is already a long/int.
This is worked for me
Thank you
Related
I want to generate date in the format 2019-03-28T15:30:59+12:00 using wiremock.
I tried:
"currentDateTime": "{{now timezone='Australia/Sydney' format='yyyy-MM-dd'T'HH:mm:ssZ'}}"
but, I get exception:
wiremock.com.github.jknack.handlebars.HandlebarsException: inline:
found: ''yyyy-MM-dd'T'HH:mm:ssZ'', expected: 'no viable alternative at
input ''yyyy-MM-dd'T'HH:mm:ssZ'''
I have also tried escaping both the quotes around T, but it does not work.
What I am doing wrong?
Try using following block of code. Working fine for me and probably the best way.
format='yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX'
In case anyone else comes across this post in the future, the simplest (but hacky) solution that I used is to format the parts before and after the 'T' separately like so:
"currentDateTime": "{{now timezone='Australia/Sydney' format='yyyy-MM-dd'}}T{{now timezone='Australia/Sydney' format='HH:mm:ssZ'}}"
A simple workaround:
Declare a variable (myformat in the example)
{{#assign 'myformat'}}yyyy-MM-dd'T'HH:mm:ssZ{{/assign}}
Use it in the stub/mock
{{now format=myformat}}
A sample templated response is -
{
"time": "{{#assign 'myformat'}}yyyy-MM-dd'T'HH:mm:ssZ{{/assign}}{{now format=myformat}}"
}
References
How to use custom variables in Handlebars for WireMock
https://wiremock.org/studio/docs/response-templating/misc-helpers/#assignment
If you need to include the date in the ISO 8601 format, you can omit the format option:
{{now timezone='Australia/Sydney'}}
It will produce the following result: 2021-06-09T05:45:53+10:00. If you omit the timezone and just use {{now}} - it will produce a date in UTC: 2021-06-08T19:48:27Z.
For reference, you can check the RenderableDate class in wiremock
"body": "{\"datetime\": \"{{now format='yyyy-MM-dd\\'T\\'HH:mm:ss'}}\"}"
Is there an easy way to use logger.entry(p1,p2,p3) from log4j 2 with the parameter names included in the output?
logger.entry(p1,p2,p3)
Result:
... entry params(val1, val2, val3)
But shut result in:
... entry params(p1=val1, p2=val2, p3=val3)
Edit:
The problem with a simple solution like
logger.info(p1+" "+p2+" "+p3);
or
logger.entry("p1="+p1,"p2="+p2,"p3="+p3)
is that the string is build before every function call and results in a performance lose. I ask me, if there is a build in way in log4j.
You can try something like this:
logger.entry("p1="+p1,"p2="+p2,"p3="+p3)
I hope someone could help me with some issue.
I'm using OWASP ESAPI 2.1.0 with JavaEE, to help me to validate some entries in a web application. At some point I needed to validate a Windows file path, so I added a new property entry in the 'validation.properties' like this one:
Validator.PathFile=^([a-zA-Z]:)?(\\\\[\\w. -]+)+$
When I try to validate, for example, a string like "C:\TEMP\file.txt" via ESAPI, I get a ValidationException:
ESAPI.validator().getValidInput("PathFile", "C:\\TEMP\\file.txt", "PathFile", 100, false);
Alternatively, I also tried the java.util.regex.Pattern class to test the same regular expression with the same string example and it works OK:
Pattern.matches("^([a-zA-Z]:)?(\\\\[\\w. -]+)+$", "C:\\TEMP\\file.txt")
I must say that I added other regex in 'validation.properties' and worked OK. Why this one is so hard? Could anyone help me out with this one?
This is happening because the call to validator().getValidInput("PathFile", "C:\\TEMP\\file.txt", "PathFile", 100, false); wraps a call to ESAPI.encoder().canonicalize() that is transforming the input to the char sequence (Not literal String!) C:TEMP'0x0C'ile.txt before it passes to the regex engine.
Except for the second "\" getting converted to the char 0x0c this is normally desired behavior. That could be a bug in ESAPI.
What you want, is to make a call to ESAPI.validator().getValidDirectoryPath()
I need to replace the ROOMS start and end tag from an xml file.
<A><ROOMS><B></B></ROOMS></A>
becomes
<A><B></B></A>
And also
<A><ROOMS><B></B></ROOMS></A>
becomes
<A><B></B></A>
I tried
Pattern.compile("\\\\\\\\<(.*)ROOMS\\\\\\\\>").matcher(xml).replaceAll("")
, but it does not work.
Can anybody help me?
Your regex is absurd. Just use:
xml = xml.replaceAll( "</?ROOMS>", "" );
Try using
<[/]?ROOMS>
as your pattern. It uses the ? flag to indicate that the XML-closing forward slash should occur 0 or 1 times.
You can probably use this regex :
<[\/]?ROOMS>
I'm trying to add a jQuery post to some JavaScript on a web page. The entire page is built up of several Velocity templates. Everything has been fine until I've tried to add the jQuery post, now I get:
org.apache.velocity.exception.ParseErrorException: Encountered "," at line 282, column 24 of /WEB-INF/velocity/www/comments.vm
Was expecting one of:
"(" ...
<RPAREN> ...
<ESCAPE_DIRECTIVE> ...
~~~snip~~~
Line 282 is $.post(... and column 24 appears to be the first "," character. Initially I had the JSON on this line, but I moved it up (to the var myJSONObject ... line)as I thought the error related to invalid JSON (tabs at the start of the line gave a misleading column number).
var myJSONObject = {"body": "", "action": "postcomment", "submitted": "true", "ajax": "true"};
myJSONObject.body = $("body").val();
$.post("$!{articleurl}", myJSONObject, function(result){
btn.textContent='Comment sent successfully.';
});
Minor Update
I changed the following lines:
var url = "$articleurl";
$.post(url, myJSONObject, function(result){
~~~snip~~~
The parse exception still focuses on the first ",". I'm assuming the issue is that Velocity thinks it should be able to resolve $.post - when in fact, it's jQuery. I've used jQuery in other Velocity VM templates without any problem. Is there a way to get Velocity to ignore certain lines / statements when parsing?
Update 2
I found this link about escaping references in Velocity, but it does not resolve my issue. Adding a "\" before $.post gives me the exact same error, but the column is one extra, because of the character added at the start of the line.
You can wrap your javascript with #[[ ... ]]# which tells Velocity to not parse the enclosed block (new in Velocity 1.7)
#[[
<script>
...
</script>
]]#
Ok, there appears to be two solutions for this:
First, with jQuery we can just avoid using the global alias $ and instead use the jQuery object directly:
jQuery.post(url, myJSONObject, function(result){
~~~snip~~~
In my case, the above works great. But I suspect in other scenarios (non-jQuery) this may not be possible. In which case, we can 'hide' our character within a valid Velocity reference like this:
#set( $D = '$' )
${D}
Source: http://velocity.apache.org/engine/devel/user-guide.html#escapinginvalidvtlreferences
I'd still like to know why the backslash escape didn't work, but the above will at least get me moving again. :)
I think this is a bug in version 1.6.x, because it works fine in 1.7(If it did not, please tell me, I test it many times..), according to the reference, the $ takes effect only when it is followed by a-zA-Z. I want to try do debug what happened really, but the translation code is generated by Java CC tool, it is too hard to recognize the logic...
you must create a js file with your javascript code
and import your js file into your vm code
I couldn't get it to work with any of the other fixes like escaping "$" in velocity unfortunately. I got it working by loading an external js-file with the jQuery instead of writing jQuery directly in velocity. Worked out for me at least, hope it helps someone :)
/björn