Howto embed a custom non-visible HTML5 data attribute in a Freemarker template? - spring

I would like to define a custom data-* attribute in an Spring form tag:
<#form.input path="endDate" data-tralaaa="moin"/>
Unfortunately Freemarker doesn't like this. Anyone a clue? I get the following exception:
Caused by: freemarker.core.ParseException: Encountered "-" at line 24, column 114 in WEB-INF/views/reisenachsendung/period.ftl.
Was expecting:
"=" ...
I would like to produce a HTML5 valid page. So dataTralaaa is not an option.

Update: As of 2.3.22, you can use - (and . and :) as part of any name if you precede it with a \, like in <#form.input data\-tralaaa="moin">. (It's not too cute, but - is already used as subtraction operator, and fixing this wouldn't backward compatible, and so must wait for a major FTL version increase.)
The problem is that you can't use dash in parameter names. This is a syntactical restriction. But that's maybe only half of the problem. Does Spring's #form.input accept parameters that it doesn't know and inserts them into the HTML tag? Because if it doesn't, then the whole dash issue doesn't mater.

Related

How to use Power Automate "uriQuery()" function on an url that contains special characters

Due to some odd circumstances I have the necessity to use uriQuery() in a Power Automate flow in order to extract the query string from an url.
This works as expected in most circumstances, except when the url contains special characters like accented letters, for example
http://www.example.com/peppers/Jalapeño/recipe #1.docx
In such cases the call triggers an error and the exception message shows a (partially) encoded version of my url (why?).
The template language function 'uriQuery' expects its parameter to be a well-formed absolute URI. The provided value was '......'
Obviously the url was indeed a well-formed, absolute URI.
Since the error only triggers when the url contains special characters I assumed that I had to encode the value before calling uriQuery(), yet nothing I tried seems to work (for example encodeUriComponent() ). And as expected nothing I could find on the web mentioned a similar issue.
As a last attempt I am asking here - does uriQuery() support this use-case? And if it does... how?

Ajax.oncomplete escaping the singlequote incorrectly

I'm using the Omnifaces ajax.oncomplete function to show a toastr message in my JSF 2 page. The problem I'm facing is that I'm developping a frensh web application and we use a lot of quotes ('). When I add the quote, the browser throws a malformed XML exception :
malformedXML: missing ) after argument list
While I'm using the braqueted slash to make it ignore the quote and treat it as a string :
Ajax.oncomplete("toastr.warning('Vérifier l\'adresse e-mail saisie.')");
Is there a way to pass this exception ?
The \ is also an escape character in Java itself. So, ultimately the \ got stripped off by Java.
You need to double-escape it to represent a literal \, so it arrives as a real \ in JavaScript.
Ajax.oncomplete("toastr.warning('Vérifier l\\'adresse e-mail saisie.')");
Alternatively, if those strings are not hardcoded and thus coming from a dynamic source, and you'd basically thus need to perform automatic escaping, then better use Ajax#data() to automatically let OmniFaces encode the Java variable as a properly formatted JSON object available via OmniFaces.Ajax.data in JavaScript context.
Ajax.data("message", "Vérifier l'adresse e-mail saisie.");
Ajax.oncomplete("toastr.warning(OmniFaces.Ajax.data.message)");
This way you don't need to worry about escaping fuss.

How to output ${expression} in FreeMarker that contains HTML AS HTML

In my data model myVar contains <b>hello</b> and when I bring it like this ${myVar} the output I get is literally <b>hello</b> rather than hello.
Any idea how to correct this?
Certainly you have HTML escaping on, so try <#noescape>${myvar}</#noescape>.
Update: Since 2.3.24 a new kind of auto-escaping was introduced, which doesn't use #escape, and hence nor #noescape. When that's used, write ${myvar?no_esc} to avoid escaping. Or, you can put the value into the data-model already as a TemplateHTMLOutputModel (created with HTMLOutputFormat.fromMarkup(myString)), and then FreeMarker will know without ?no_esc that it need not be escaped.

Escaping special characters inside XPATH

I have some elements with the tag <xxx:element> inside my xml.
I want to get these using XPath. I've tried a few ways of getting them but so far unsuccessful.
//xxx:element just doesn't return anything. I'm guessing this is because of the : characater
//#xxx:element# gives the exception: "A location step was expected following the '/' or '//' token."
//'xxx:element' same exception.
Any suggestions?
Based on choroba's answer I found Xml Namespace breaking my xpath!
The xxx: part is a namespace prefix. It should work in XPath, but depending on the language you are using, you might need to register the namespace before you can use it.
Found a solution using the local-name property. The following works just fine:
//*[local-name()='element']
In saxon //*:element do the trick to me

Trouble using Xpath "starts with" to parse xhtml

I'm trying to parse a webpage to get posts from a forum.
The start of each message starts with the following format
<div id="post_message_somenumber">
and I only want to get the first one
I tried xpath='//div[starts-with(#id, '"post_message_')]' in yql without success
I'm still learning this, anyone have suggestions
I think I have a solution that does not require dealing with namespaces.
Here is one that selects all matching div's:
//div[#id[starts-with(.,"post_message")]]
But you said you wanted just the "first one" (I assume you mean the first "hit" in the whole page?). Here is a slight modification that selects just the first matching result:
(//div[#id[starts-with(.,"post_message")]])[1]
These use the dot to represent the id's value within the starts-with() function. You may have to escape special characters in your language.
It works great for me in PowerShell:
# Load a sample xml document
$xml = [xml]'<root><div id="post_message_somenumber"/><div id="not_post_message"/><div id="post_message_somenumber2"/></root>'
# Run the xpath selection of all matching div's
$xml.selectnodes('//div[#id[starts-with(.,"post_message")]]')
Result:
id
--
post_message_somenumber
post_message_somenumber2
Or, for just the first match:
# Run the xpath selection of the first matching div
$xml.selectnodes('(//div[#id[starts-with(.,"post_message")]])[1]')
Result:
id
--
post_message_somenumber
I tried xpath='//div[starts-with(#id,
'"post_message_')]' in yql without
success I'm still learning this,
anyone have suggestions
If the problem isn't due to the many nested apostrophes and the unclosed double-quote, then the most likely cause (we can only guess without being shown the XML document) is that a default namespace is used.
Specifying names of elements that are in a default namespace is the most FAQ in XPath. If you search for "XPath default namespace" in SO or on the internet, you'll find many sources with the correct solution.
Generally, a special method must be called that binds a prefix (say "x:") to the default namespace. Then, in the XPath expression every element name "someName" must be replaced by "x:someName.
Here is a good answer how to do this in C#.
Read the documentation of your language/xpath-engine how something similar should be done in your specific environment.
#FindBy(xpath = "//div[starts-with(#id,'expiredUserDetails') and contains(text(), 'Details')]")
private WebElementFacade ListOfExpiredUsersDetails;
This one gives a list of all elements on the page that share an ID of expiredUserDetails and also contains the text or the element Details

Resources