struts2 s:select value ognl expression - struts2

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

Related

Struts2 Iterate value from a textfield

I am trying to iterate over the textfield's value, for eg. 5. Is there a way to get the value?
I have a textfield with value 5:
<s:textfield
theme="simple"
cssClass="form-control"
name="instCount"
value="5"
style="width:25%;"
onkeyup="javascript:isNumber(this);"
/>
I need to iterate the value 5:
<s:if test='<s:property value="%{instCount}"/> > 0'>
<s:iterator value='<s:property value="%{instCount}"/>' var="count" status="countStatus">
</s:iterator>
</s:if>
You cannot nest JSP tags like that, and it appears that you haven't wrapped your head around OGNL or ELs in general, even though you use it correctly in the <s:property> tag.
Let's take a step back: what is this doing?
<s:property value="%{instCount}" />
It's referring to an action property named instCount.
How? Via the OGNL expression %{instCount}.
How does the <s:if> tag work? By evaluating an OGNL expression in the test property. Ah, OGNL expression, which we've already seen.
<s:if test='instCount > 0'>
How does the <s:iterator> tag work? By evaluating an OGNL expression in the value tag.
<s:iterator value='%{instCount}' etc...>
I would highly recommend taking some time to figure out the framework you're working in, even a minor reading of the documentation (and a basic understanding of how JSP works) will be highly beneficial, and avoid questions like this.

Create Struts2 select tag with Enumsubset

I have a JSP page and I use Struts to build my select tag. The JSP code is the following:
<s:select
required="true"
name="form.tipologia"
label="%{getText('Enum.label')}"
list="#it........Enum#values()"
listKey="name()"
listValue="getText('Enum.' + name())"
headerKey=""
headerValue="%{getText('Enum.')}"
/>
This code produces me a select field with my Enum constants.
What I want to do is to create the field with only a subset of the Enum.
How can I do it? Is it possible?
You can use OGNL projection for this.
<s:select list="#it..Enum#values().{? #this != #it..Enum#ENUM_TO_EXCLUDE}" />
This will create a subset of all enum values except that one that you want to exclude.
If comparing enums doesn't work then you can compare strings.
<s:select list="#it..Enum#values().{? #this.toString() != 'ENUM_TO_EXCLUDE'}" />

Evaluating a run time expression in Struts2 tag

The following tag is faulty
<s:textfield name="st_enroll.name" value="getText('st_enroll.name')" theme="simple" />
Since you cant insert expression in struts2 tags i read that by using getText you could insert expressions but that doesn't work. Any idea how i could insert the expression ${st_enroll.name} in the value field of the struts2 tag ?
you should wrap the method using '%{}'
<s:textfield name="st_enroll.name" value="%{getText('st_enroll.name')}" theme="simple" />

struts 2 text tag with dynamic expression , how?

I need to supply the key dynamically from the action to the text tag
<s:text name="<%=talkToUsInfo.messageKey%>" />
but the name attribute on text tag is declared as false ( not sure why ? )
How do i get something like this working without changing the tld in the jar file ?
Take a look at OGNL
It might look like this
<s:text name="%{talkToUsInfo.messageKey}" />
Struts documentation says:
Instead, you should use the getText() method that you inherit when your Action extends XWork's ActionSupport:
<s:textfield name="lastName" label="getText('person.lastName')" />
So I used e.g.
<s:property value="getText('status' + #someObject.currentStatus)" />
instead of "s:text" and it worked.
I prefer to use OGNL and s:text and do not make a call to getText manually.
You usually define a fixed prefix while add the dynamic part to it, so some thing like this could be used:
<s:text name="%{'student.types.'+currencyType}" />
The only time which I used getText inside the s:property was something like this (which needs some if/else if you wanted to use s:text ):
<s:property value="shouldCancelIt.equals(\"0\") ?
getText('label.yes'):getText('label.no')" />

Difference between '#','%' and '$'

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.

Resources