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'}" />
Related
Is it possible to define a Struts Select field, <s:select>, with an Iterator for options, <s:iterator>?
e.g., I don't want to use the Key/Value/List properties,
<s:select id="criteriaRequestStatusList" name="searchRequestCriteria.requestStatusList"
list="requestStatuses"
listValue="description" listKey="id" />
because I have some special symbols such as coming from the server side and they aren't escaped.
The following works regarding escaping , but it's an HTML Select. I don't want to use this either, because it doesn't populate the form values properly on load.
<select id="criteriaRequestStatusList" name="searchRequestCriteria.requestStatusList" class="requestor input-block-level" name="requestors" multiple="multiple">
<s:iterator value="requestStatuses">
<option value="${id}">${description}</option>
</s:iterator>
</select>
My goal is,
<s:select ..>
<s:iterator>
You can set the value on the POHTML select using the normal value attribute.
The <s:select> tag expects values to be ready-to-use; personally I'd transform them on the server side before exposing them to the view layer.
You might be able to use OGNL in the listTitle property (I might have the property name wrong) to un-entity it; I don't recall.
I am using struts2 for presentation layer, now there is requirement to use html5 tags e.g. email, tel etc., but it seems that struts2 doesn't support html5 tags.
Is there any way to achieve the above requirement?
You can add custom (HTML5 and others) attributes to Struts2 Tags that have declared they support Dynamic Attributes. Look in the documentation, for each tag you are using, under the Parameters part if you see Dynamic Attributes Allowed: true;
You can still use native HTML elements, along with Struts tags. Then both the following ways are good:
<s:textfield name="foo" value="bar" customAttr />
<input type="text" name="foo" value="<s:property value="bar"/>" customAttr />
If you need to change the type, you can now do it with the Struts <s:textfield /> tag too:
<s:textfield type="email" name="foo" value="bar" customAttr />
<s:textfield type="date" name="foo" value="bar" customAttr />
<s:textfield type="currency" name="foo" value="bar" customAttr />
etc...
Your struts2 tags(<s: >) will finally be converted to basic HTML tags when the page is rendered, which you can check by looking into the generated source. Usually, Struts2 just adds some css(if you are not using theme="simple") to the basic HTML tags. So. it is incorrect to say that
struts2 doesn't support html5 tags
Use the Html5 tags as usual, they will work.
It looks like you are new to the community, so dont get demotivated by the downvotes but in future try to do some homework and ask specific problems, what did you try and what did not work
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
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" />
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')" />