First things first, I'm not a seasoned Struts user. Here is my struts code:
<s:set var="foo" value="BAR">
<s:text name="key.for.foo">
<s:param><s:property value="foo" /></s:param>
</s:text>
Here is the properties file containing the key.for.foo text value:
key.for.foo=blah blah {0}
I expect the text below:
blah blah BAR
but I get
blah blah
What am I missing ?
You can try the below code
<s:text name="key.for.foo"><s:param value="#foo"/></s:text>
The usage of <s:set /> vars is covered in the tag documentation's example:
<s:set var="personName" value="person.name"/>
Hello, <s:property value="#personName"/>
Nutshell is that the Value Stack has multiple ways of looking things up: stuff pushed onto the stack (e.g., the action itself, <s:push /> properties), stuff in the servlet context (e.g., session and request attributes/properties), and named objects (e.g., <s:set /> objects, <s:iterator /> status value).
Related
<c:set var="xmlDocumentId" value="${id}" scope="request" />
<s:set var="xmlDocumentId" value="%{id}" scope="request" />
are formatting id based on locale, setting xmlDocumentId to "12,345" while:
<c:out value="${id}" />
<s:property value="%{id}" />
are outputting "12345".
Any ideas how to break this behavior?
Because you are getting value with getText or <s:text> tag your long value gets formatted according to locale. To prevent this from happening convert your long to string.
With <s:set> tag you can call toString() method directly in value attribute.
<s:set var="xmlDocumentId" value="id.toString()" scope="request" />
For the concrete formatting algorithm take a look at java.text.MessageFormat class and its subformat method.
Once you know how to format numbers in Java, in Struts2 <s:property/> tag you can use getText() to format your number in the desired way, for example :
<s:property value="getText('{0,number,#,##0}',{id})"/>
Try this
<s:text name="id" > <s:param name="value" value="id"/> </s:text>
I am trying to set the text in a button to either 'Enable' or 'Submit' depending on the value of a action class member(mode). But it is reporting an error which is saying 'equal symbol expected' on the first line. I searched and found that there are questions regarding 'equal symbol expected' error but nothing specific to Struts 2 tags. Neither I could spot any obvious error as missing closing quotes.
It would be nice if anyone can help.
<s:set name="submitButtonLabel" value="<s:if test="mode.equals('enable')">Enable</s:if> <s:else>Submit</s:else>" />
<s:submit value = "%{#submitButtonLabel}" cssClass="btn btn-gray" />
Try this:
<s:submit value="%{mode.equals('enable') ? 'Enable' : 'Submit'}" />
You cannot nest tags like that. Write your <s:if> inside <s:set> tag instead.
<s:set name="submitButtonLabel">
<s:if test="mode.equals('enable')">Enable</s:if>
<s:else>Submit</s:else>
</s:set>
I'm trying unsuccessfully to use a Struts2 message resource in a tiles
2.1.4 expression. For example the following resource is available in
the JSP...
<s:text name="htmlheadHeading1"/><br/>
... but when I attempt to use the same resource in the tiles definition...
<put-attribute name="title" expression="${htmlheadHeading1}" />
... I get the error...
ELResolver cannot handle a null base Object with identifier 'htmlheadHeading1'
... Is there any way to do this??
This solution does not use tiles expressions but will work if you need to display localized text in JSP when key is passed through tiles definitions.
In tiles definition:
<put-attribute name="title" value="htmlheadHeading1" />
In your JSP then use Struts2 <s:set> tag to set title from tiles definition to local variable and then use it in <s:text> tag.
<s:set var="title">
<tiles:getAsString name="title" ignore="true"/>
</s:set>
<s:text name="%{#title}"/>
Hello I am a young Software developer,
and I struggled the last 5 days with my code.
Here is my code in JSP:
<s:iterator value="getListeDanach()" status="stat">
<li>
<s:url id="URL_ListeDanach" action="uebersicht_umblaettern">
<s:param name="angeklickteSeitenzahl" value="getListeDanach()[#stat.index]" />
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="last" value="3" />
</s:bean>
<s:iterator value="#counter" status="stat1">
<s:property value="#stat1.index" />
<s:param name="%{optionaleParamName4}" value="#optionaleParamValue4" />
</s:iterator>
</s:url>
<s:a href="%{URL_ListeDanach}" cssClass="naviTab">
<s:property value="getListeDanach()[#stat.index]" />
</s:a>
</li>
</s:iterator>
My problem is, the first Iteration works great but the 2nd Iteration works half. In the 2nd case the property works, but the param doesn´t work! Al Variables are available. If i take the param Tag of the 2nd Iteration and place it in the first, it works great! But that isn´t what I want.
This is not an answer.
Here's the JSP, cleaned up, and using more S2 functionality. It was impossible to read the original.
<s:iterator value="listeDanach" status="stat" var="outerItem">
<li>
<s:url id="URL_ListeDanach" action="child">
<s:param name="angeklickteSeitenzahl" value="outerItem" />
<%-- What are you trying to do here? --%>
<s:bean name="org.apache.struts2.util.Counter" var="counter">
<s:param name="last" value="3" />
</s:bean>
<%-- What are you trying to do here? There's nothing to iterate over. --%>
<s:iterator value="#counter" status="stat1">
<s:property value="#stat1.index" />
<s:param name="%{optionaleParamName4}" value="#optionaleParamValue4" />
</s:iterator>
</s:url>
<s:a href="%{URL_ListeDanach}">
<s:property value="outerItem" />
</s:a>
</li>
</s:iterator>
In the bean i have a loop with 3 "rounds", in the 2nd iterator i use the var=counter to iterate three times over the property and over the dynamic parameter.
The property shows in HTMl in every loop of the first iterator this result : 0 1 2;
This is how it should work(the property is just there, to test the functionality of the 2nd itarator.)
But in the 2nd case, the parameter-Tag is fully ignored or something like that. For those who want to know the logic behind the code. It is a side navigation bar. listeDav or = list behind the actual number, and listeDanach= list after the actual number. 1 2 3 4 [5] 6 7 8 9.... When the param Tag in the 2nd itarator functions well, I would make the param tag dynamically with the iterated index.
SO in short, what I want is: Every time the first Iterator has his loop, I want to create dynamic parameters. This parameters are defined in the JSP before and are fully supported! I want to use the index "#stat1.index" to make it work.
Something like this :
s:param name="%{optionaleParamName[#stat1.index]}" value="#optionaleParamValue[#stat1.index]" />.....
i have already defined the String behind "#optionaleParamValue[0], behind #optionaleParamValue[1] and behind #optionaleParamValue[2] and soo on... ... and all this is for reusing the actual JSP.
As you can mention, a side navigation bar, can be used in many other cases in the programm.
Greetings
Using Grails' GSP <g:set> tag, is it possible to specify the type of the variable? I want to declare an integer variable, but <g:set> always declares a sting. For example:
<g:set var="x" value="100"/>
${x.getClass()}
${x+23}
results in
class java.lang.String
10023
I'd like to declare x as an integer. I noticed that using the JSP tag <% int x=100; %> results in:
class java.lang.Integer
123
Is there a way to do this the Grails/GSP way?
Use the ${} syntax when defining the value. For example:
<g:set var="x" value="${100}"/>
You can see the tag doc for g:set for more info.
Just as an additional comment for someone who comes across this since it is the only useful result on the Internet for and casting/Int/Sring/etc. This example works in the case of variables:
<g:set var="printLeft" value="${offer?.metaInfo?.redeemPrintY as Integer}"/>
<g:set var="printTop" value="${offer?.metaInfo?.redeemPrintX as Integer}"/>
<g:set var="printWidth" value="${offer?.metaInfo?.redeemPrintW as Integer}"/>
<g:set var="printHeight" value="${offer?.metaInfo?.redeemPrintH as Integer}"/>
...
<area shape="rect" coords="${printLeft},${printTop},${printLeft+printWidth},${printTop+printHeight}" onClick="printOffer();" />