I can't find on the tutorial the difference between this two instructions.
th:field="${something}" and th:field="*{something}"
Can anyone show me some example?
Reference site
Five types:
${...} : Variable expressions. These are OGNL expressions (or Spring
EL if you have spring integrated)
*{...} : Selection expressions. Same as above, excepted it will be executed on a previously selected object only
#{...} : Message (i18n) expressions. Used to retrieve locale-specific messages from external sources
#{...} : Link (URL) expressions. Used
to build URLs
~{...} : Fragment expressions. Represent fragments of
markup and move them around templates
Related
is there a way to loop from 'a' to 'z' using th:each block of thymeleaf in a similar way we do using #numbers.sequence?
For the moment I am using a static array of chars in the back-and and passing this to the front-end.
You can do the following:
<div th:with="letters='abcdefghijklmnopqrstuvwxyz'"
th:each="num : ${#numbers.sequence(0, 25)}">
<div th:text="${#strings.substring(letters, num, num+1)}"></div>
</div>
This still requires a hard-coded string (as you can see), which you could just as easily pass to Thymeleaf as a Java String (not as an array of chars). But maybe it's more acceptable than what you are currently doing.
I don't know of any way in which Thymeleaf can directly use Java's (char)('A' + num) technique - otherwise that would probably be what you are looking for. I think that is not possible. Unless/until someone proves it is possible.
Update
To prove myself somewhat wrong, I used the following approach:
<div th:each="num : ${#numbers.sequence(97, 122)}">
<div th:text="${#conversions.convert(num, 'java.lang.Character')}"></div>
</div>
This prints a through z by converting the decimal ASCII values to Java chars.
However this is only valid if you are using the Spring dialect of Thymeleaf - which is not mentioned in your question - so may be of no help to you.
A solution using the Thymeleaf standard dialect (no Spring) is to define a custom conversion service.
That requires more coding than your current approach - so again, is probably not what you want. But I mention it just in case. How you implement this depends more specifically on how you have integrated Thymeleaf into your program.
I came across some .xhtml files where for some components the expression language used was like rendered="#{empty from}", rendered="#{empty create}" etc.
I know that empty is an operator in EL, used to check whether a value is null or empty, but I did not understand the meaning of above mentioned ELs.
Can somebody explain to me what above EL's mean?
The rendered attribute is a signal whether JSF should generate HTML for the component or not. If it evaluates false, then it won't generate HTML for the component (nor for its children). The #{empty form} will evaluate false when the #{form} is not null nor empty.
Simple as that. You can find another examples of boolean expressions here: Conditionally displaying JSF components
I often have Javascript on my JSF XHTML pages that has && in it, which I end up having to encode as &&
For example, when I place the following in my JSF XHTML page file:
I am an & sign
I get the error:
The entity name must immediately follow the '&' in the entity reference
One wayto fix this appears to be to change the '&' to & which I find undesirable to just writing '&'.
It also appears that for cases where I use the '&' in Javascript, I can wrap the Javascript in CDATA tags; when wrapped in CDATA tags, I can then write '&' without having to escape it as &, which is a good workaround to be able to have more readable Javascript code on my page.
But what happens when I want to use the literal '&' elsewhere on the page when it is not within <script> tags and therefore cannot as easily wrap the code in CDATA tags? Must I always escape '&' as & for these cases?
Note trying to use 's ability to escape values and do not seem to be able to fix the issue
Facelets is a XML based view technology. Any characters which have special treatment by the XML parser needs to be XML-escaped when the intent is to present them literally. That covers among others < and &. The < indicates the start of a XML tag like so <foo> and the & indicates the start of a XML entity like so &. The < must be escaped as < and the & as &.
Not escaping them in Facelets would result in the following exception for <
javax.faces.view.facelets.FaceletException: Error Parsing /test.xhtml: Error Traced[line: 42] The content of elements must consist of well-formed character data or markup.
and the following one for &
javax.faces.view.facelets.FaceletException: Error Parsing /test.xhtml: Error Traced[line: 42] The entity name must immediately follow the '&' in the entity reference.
This is not specifically related to JavaScript, this applies to the entire view, including "plain text". Those characters just happen to be JavaScript operators as well. There's no way to go around this, that's just how XML is specified. In JavaScript, there's however one more way to avoid escaping or using CDATA blocks: just put that JS code in its own .js file which you load by <script> or <h:outputScript>.
In EL, there is also the && operator which also needs to be escaped as && as well, but fortunately there's an alias for this operator, the and operator.
See also:
Mozilla Developer Network - Writing JavaScript for XHTML
It's because & is special characters in XML : http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
FYI, I tried to write the (c) character in my JSF page.
An error was raised when I wrote © : "copy is referenced but not declared"
When I wrote © I got the raw string back.
I could display the special character using the unicode notation : ©
This code worked for me :
<h:outputText value="©" escape="false" />
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()'}"
I would like to parse standard security expressions like hasRole etc. to get their value in a variable (in a JSP). How can I do that? SpelExpressionParser is the standard EL parser, but I don't think it will parse the security expression.
The simpliest approach is to use a <sec:authorize> tag and modify a desired variable inside its body.
If you actually want to evaluate expressions manually, look at the source of AuthorizeTag - it gets the first bean of type WebSecurtyExpressionHandler from the web application context and uses it to obtain ExpressionParser and EvaluationContext.
I've posted an answer in this topic here - How to parse spring security expressions programmatically. I think this answers your question also.