struts 2: equal symbol expected error - struts2

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>

Related

How to pass a variable to s:text?

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).

JSTL c:set and Struts s:set are undesirably formatting numbers

<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>

Struts 2 ApplicationResources.properties error

I am using struts2 for my application.
<s:submit cssClass="button" key="btn.search" tabindex="12" />
in ApplicationResources.properties file i have
btn.search = Go
and i am getting error while submitting the page,
OgnlValueStac W com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
Error setting expression 'btn.search' with value '[Ljava.lang.String;#14f414f4'
ognl.OgnlException: target is null for setProperty(null, "search", [Ljava.lang.String;#14f414f4)
What's the problem?
The key attribute is a shorthand for both the name and value attributes.
Using key means you're assuming a property named btn.search.
While you may set a value to the results of a text property lookup, you can also use it directly:
<s:submit value="%{getText('btn.search')}" />
The key is being submitted as a parameter and OGNL is trying to get an object named btn from the ValueStack to set the parameter by calling getBtn().setSearch("");, but, since you have no btn object in the stack, the null target exception is occurring.
This should work:
<s:set name="buttonText"><s:text name="btn.search"/></s:set>
<s:submit cssClass="button" value="%{#buttonText}" tabindex="12"/>
Or, as Dave has pointed out in his answer, this should work as well if your action class extends ActionSupport:
<s:submit cssClass="button" value="%{getText('btn.search')}" tabindex="12"/>

Struts 2 iterator tag status index is not working

<s:iterator value="podTemplate.subTypeTemplates" status="subTemplate">
<s:textfield id='subType_type_#subTemplate.index' key="subType" label="Name"/>
</s:iterator>
#subTemplate.index is not getting replaced by index. However if I am doing
<s:property value="#subTemplate.index"> is working
That's because id attribute of textfield is of string type and string-type attributes are not interpreted as OGNL by default. As Steven said you have to force the interpretation by using %{} in your case subType_type_%{#subTemplate.index}.
My iterator looks like this:
<s:iterator id="list" value="optionList" status="rowStatus" >
I tried:
<td>
<input class="textInput required"type="text" name="optionList[${rowStatus.index}].option_id"/>
</td>
and it's the only one that worked.
All the following ones failed:
name="optionList[%{#rowStatus.index}].option_id"
name="%{optionList[#rowStatus.index].option_id}"
even
name="%{#rowStatus.index}"

Grails GSP <g:set> tag set as integer?

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();" />

Resources