Dynamic variable name with dashes in thymeleaf - thymeleaf

I am trying to create a variable name in thymeleaf by using another variable as prefix:
th:with="appOverview = ${__${tabId}__AppOverview}"
It works most of the times, but in the case where tabId contains dashes (i.e., "my-test-tab"), thymeleaf throws the following exception:
The operator 'SUBTRACT' is not supported between objects of type 'null' and 'null'
I guess that the dashes confuse thymeleaf into believing it's an arithmetic operation between non-existing variables.
I tested adding a variable with th:with="my-var='bad dashes'" and it worked fine, so I suppose dashes are generally accepted as characters in variable names.
Is there a way to tackle this issue?

Ah, I finally found it!
The thing I was missing is that all model variables are stored in thymeleaf in the #vars object, so accessing them from there won't confuse thymeleaf. Here's what worked:
th:with="appOverview = ${#vars.get('__${tabId}__AppOverview')}"

Related

thymeleaf 3 and th:field syntaxe

I have a form template using thymeleaf 3 and I want to use th:field="*{dueDate}" syntax to bind my model property. I try to do the same as th:value="${#strings.substring(model.dueDate, 0, 19)}" but all my attempts fail.
It is possible to do this into a th:field instead of a th:value ?
EDIT:
I have to substring my date from java.sql.timestamp because it's a little bit different as the format used by my datepicker (2020-01-30 13:00:00.0 instead of 2020-01-30 13:00:00). I understand it isn't a good idea to substring the date string but It is difficult to have a entity method to bind the value as well with th:field. Probably I will use th:value in this case to give me the possibility to create that entity method providing the good format.

Grails print each in attribute[]: [1,2,3]

Below, is what returning on my server:
[test[]:[1, 2, 3, 4, 5], action: something, controller: myController]
And I can't println using
params.test.each{ println it}
How can I print on each element in test[]?
The issue you are having is that the key named test[] needs to be escaped.
Thus you should be able to do:
params."test[]".each { println it }
The reason for needing to use double or single quotes around the property name is due to the fact you have brackets in the name of the property. Usually brackets appearing in Groovy code would be seen as a collection and without an index or key within the brackets it's considered invalid syntax.
So, in this case since the name of the property/key within your params contains brackets you need to wrap it in quotes so Groovy understands it's just part of the name/key and not an attempt to access it as a collection (even if your property is actually a collection).
Honestly, I would fix the root of the issue, meaning don't name your property test[] but rather test since it's much cleaner and clearer as to what your property actually is.

Problems getting data from XML using Nokogiri and Rails

I'm trying to get information from a XML file with Nokogiri. I can retrieve file using
f = File.open("/my/path/file.xml")
cac=Nokogiri::XML(f)
And what a get is a fancy noko:file. My row tags are defined like
<z:row ...info..../>
like
<Nokogiri::XML::Element:0x217e7b8 name="z:row" attributes=[#<Nokogiri::XML::Attr:0x217e754 name="ID_Poblacio" value="3">
and I cannot retrieve the rows using either:
s=cac.at_xpath("/*/z:row") or
s=cac.at_xpath("//z:row") or
s=cac.at_xpath("//row") or
s=cac.at_xpath("z:row")...
Probably I'm really fool but I cannot figure out which can be the issue.
Does anyone face this problem?
Thanks in advance.
P:S I tried to paste my cac file directly from bash but something wierd happens with format so I remove it from question. If anyone can explain how to do it I will appreciate it.
Your XML element name contains a colon, but it is not in a namespace (otherwise the prefix and uri would show up in the dump of the node). Using element names with colons without using namespaces is valid, but can cause problems (like this case) so generally should be avoided. Your best solution, if possible, would be to either rename the elements in your xml to avoid the : character, or to properly use namespaces in your documents.
If you can’t do that, then you’ll need to be able to select such element names using XPath. A colon in the element name part of an XPath node test is always taken to indicate a namespace. This means you can’t directly specify a name with a colon that isn’t in a namespace. A way around this is to select all nodes and use an XPath function in a predicate to refine the selection to only those nodes you’re after. You can use a colon in an argument to name() and it won’t be interpreted as a namespace separator:
s=cac.at_xpath("//*[name()='z:row']")

How to null check in EL to show p:dialogs conditionally?

I have two <p:dailog>s and based on the condition of a bean property I want to show one of them. I have used the following code
onclick="#{empty groupBean.selectionGroup?dialog_empty.show():groupDialog.show()}"
But it is not working as it says there is an error in EL expression. I am not sure where the error is. Am I doing it the correct way?
You're treating JavaScript code as part of the EL expression. This would only result in a syntax error because EL cannot find #{dialog_empty} nor #{groupDialog} in the scope. You have to treat JavaScript code as strings by quoting them because they ultimately needs to be written to the HTML response as-is:
onclick="#{empty groupBean.selectionGroup ? 'dialog_empty.show()' : 'groupDialog.show()'}"

Accessing a HashMap using Struts 2

I have hashmap that was created on a page using the struts2 <s:set> tag. It looks something like this
<s:set var="mymap" value="#request.mymap"/>
At some point in the page, i need to get a value from the hashmap based upon a key, and i want to do it using OGNL.
The key is generated based upon some logic, which i store using another <s:set> tag. Something like this
<s:set var="mykey" value="1">
I need to get a value from the hashmap using this key. And I need to display it.
How do I simply call the get function on the hashmap?
I tried this
<s:property value="#mymap[#mykey]"/>
and this
<s:property value="#mymap[%{#mykey}]"/>
and this
<s:property value="%{#mymap[%{#mykey}}]"/>
The third one obviously does not work because of the nesting problem.
But the same nesting logic is applicable to the second case as well, due to the manner the value attribute is handled. However none seem to work for me.
The issue here is that my key is unknown. It is a dynamically generated string based upon some pattern. I need to access the object stored in the hashmap using this dynamic key. And due to the inability of nesting ognl, I am in a fix.
I suppose the issue is very simple. I almost feel that I get it, but somehow the solution eludes me.
I suppose I was using a different version of struts wherein using the %{} was required for the expression to be evaluated. I changed the jar files now.
This is what did the job for me:
<s:property value="#mymap.[#mykey2]"/>
My problem was coming because I was trying to use it in a href for a s:a tag. And without the %{} operator, the expression was not being evaluated.
So, i guess, i was right in the beginning itself. Rest of the time, it was just me being silly. :>
Update:
I wrote a blog post on the issue, in case anyone is interested.
http://mycodefixes.blogspot.com/2010/11/struts-2-creating-and-accessing-maps.html

Resources