I'm new to struts2 and confused by the '#','%' and '$' element. There are some usages like:
${user.name}
%{user.name}
<s:radio list="#{key1:value1,key2:value2}" />
Could any give me an explanation and examples?
To put it simply
If ${user.name} in jsp page, it is an EL expression.
If ${user.name} in struts.xml, it is an OGNL expression.
If %{user.name} in jsp page, it is an OGNL expression.
Final, #{key1:value1,key2:value2} is an OGNL expression, it means creates a map that maps the key1 to the value1 and key2 to the value2.
BTW: #{key1:value1,key2:value2} should be wrap in %{}, like %{#{key1:value1,key2:value2}}, however, some attributes in struts2 tags will assume that is OGNL expression, that means without %{} is OK.
Related
How do I get thymeleaf to resolve my html entities?
I have the following:
<input th:name="title" th:value="Wayne’s World" />
will simply produce an input element with "Wayne’s World" instead of "Wayne's world".
Any way to make thymeleaf resolve the html entities?
You have two choices.
First. Do not use html entities at all. Just escape special symbols using \. Note, that you have specify string value in single quotes:
<input th:name="title" th:value="'Wayne\'s World'" />
Second. Use Thymeleaf's string utility for escape xml enitites:
<input th:name="title" th:value="${#strings.escapeXml('Wayne’s World')}" />
When your string value is coming from controller, don't use __${}__ preprocess expression. It doesn't needed. Just use Thymeleaf's standard variable expression ${}. And don't enclose this expression in single quotes. Look at ${title} variable in next example:
<th:block th:include="row::row(attrs='value=${title}, minLength=\'.{1, 16}\', required=true, ... />
In this case you can add string value in controller as is, without any escaping:
public String method(ModelMap model){
...
mode.addAttribute("title", "Wayne's world");
...
}
In a struts2 tag, the "!" negation operator doesn't seem to apply within Struts2. What am I doing wrong?
<s:if test="!%{true}">
you should NOT see this
</s:if>
You've put the ! outside of the OGNL expression:
<s:if test="%{!true}">
While the %{} is optional in this case, some will argue that it should always be used to make the OGNL evaluation explicit. I tend to agree with that, although for simple use-cases I don't always bother.
I faced a interesting problem here. When I click commandButton, it calls wrong action method even button has not action attribute. What is this?
That may happen when the EL expression which you intented to use as a method expression is been evaluated as a value expression.
For example, when you have outcommented it using a HTML comment while not having configured Facelets to skip comments.
<!-- <h:commandButton value="submit" action="#{bean.submit()}" /> -->
The HTML comments namely doesn't stop EL expressions like #{bean.submit()} from being evaluated. It will ultimately generate HTML output with the return value of #{bean.submit()} inlined in the action attribute.
To naildown the root cause in your case, you should be creating and providing a fullworthy SSCCE.
See also:
How can I remove HTML comments in my Facelets?
Outcommented Facelets code still invokes EL expressions like #{bean.action()} and causes javax.el.PropertyNotFoundException on #{bean.action}
In Struts2 tutorial for s:select tag I've seen:
<s:select label="Pets"
name="petIds"
list="petDao.pets"
listKey="id"
listValue="name"
multiple="true"
size="3"
required="true"
value="%{petDao.pets.{id}}"
/> ^ ^
and my question: why value="%{petDao.pets.{id}}"? why not simply value="%{petDao.pets.id}"? what do those trailing curly braces mean?
This is an OGNL list projection to get all the id values as a list from petDao.pets, meaning all values in this <s:select> will be pre-selected.
It isn't necessary; I suspect it was the result of an error in the tag's source file.
It works with it, but isn't needed, will fail IDE validation (if the IDE supports S2 and/or OGNL, e.g., IntelliJ), and I've made a note to update.
The principal reason is because %{} syntax is used to force OGNL evaluation where Struts would otherwise be treating the value as a String literal.
For example,
<s:property value="name" />
will look for a name property in the value stack i.e. is a value retrieved by calling getName().
If you wanted to force it to use literal value "name", you will need to use the %{} syntax -
<s:property value="%{'name'}" />
Source:
http://www.coderanch.com/t/420711/Struts/Struts
I want to parse the URL
title.xhtml?id=1
My code within "title.xhtml" should look like
...
<h:outputText value="#{titles.getTitle(${param.id}).id}"></h:outputText>
...
But unfortunatelly this does not work, since nested "#" and "$" is not accepted.
So my question is: Can I use an URL parameter and hand it over to a bean function without to store it separately within the bean?
That's invalid EL syntax. You can't and don't need to nest EL expressions in any way. Even nesting ${} is invalid. The only difference between #{} and ${} is that the #{} can perform a set operation as well (where applicable), while the ${} can do only a get operation.
This is valid EL syntax:
<h:outputText value="#{titles.getTitle(param.id).id}" />
Note that #{param.id} is fully legal JSF EL syntax. To avoid future confusions, it would be a good idea to make sure that you never use the old JSP EL syntax ${} in JSF anymore. See also Difference between JSP EL, JSF EL and Unified EL.