Struts2 taglib, compare with null - struts2

I am trying to know why this code is not working when I compare a String with null in a JSP.
<s:set name="myvar" value="%{'teststring' != null}" /> <!-- always true -->
myvar value is ${myvar}
Above code works fine, and prints "myvar value is true".
But doing any of these
<s:property value="myvar" />
<s:property value="%{myvar}" />
throws a ClassCastException
Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
So I do not know how to solve it, as I need to disable some inputs based on that variable value, ie.
<s:select ... disabled="%{myvar}" />
Thank you very much for your help.

This seems to work:
<s:property value="%{#myvar}" />

Try this
<s:property value="%{myvar.toString}" />
<s:select ... disabled="%{myvar.toString}" />

Related

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>

Build-in Grails tags do not work

During grails application upgrade from 1.3.8 to 2.3.9 I run into the problem that grails build-in tags are ignored.
For example lets say that GSP page has two tags:
<g:if test="${true}">OK</g:if>
<g:hiddenField name="test" />
then the result of execution will be:
OK
<g:hiddenField name="test" />
but expected result is :
OK
<input type="hidden" name="test" />
Basically tags from grails-web-2.3.9.jar are working fine but from grails-plugin-gsp-2.3.9.jar are completely ignored.
Any clue why this happens is appreciated. Thank you.
UPDATE:
I found that while executing TagLibraryLookup.afterPropertiesSet() method grailsApplication object is null, so the tag lib is not registered. Now it is not clear why it is null...
Solotion:
The problem was that within static constraints = { } of domain object it was a call of
ApplicationHolder?.getApplication().getMainContext()?.getBean(serviceName) method. It prevented the application from initialization of all required spring/grails beans by changing initialization sequence/flow.
You're missing some double-quotes here:
<g:if test=${true}>OK</g:if>
<g:hiddenField name="test" />
If you add them:
<g:if test="${true}">OK</g:if>
<g:hiddenField name="test" />
Does that help? Also, check for any changes to the default encoding that was made in Config.groovy after you upgraded.

Struts 2 <s:property evaluate express

I am trying to evaluate expression in <s:property tag as following..
i am able to print <s:property value="%{quoted*100)}>
but unable to print following expression
<s:property value="%{quoted*100)/Total}" />
<s:property value="%{(quoted*100)/Total}" /> missing '('

Property value in <s:textfield> struts2

How to set propety value in <s:textfield>
I tried <s:textfield name="customerName" label="Customer Name" value='<s:property value="userInfo.customerName"' /> but it didn't work.
Please help
You can not use a tag inside of a tag. Use OGNL instead!
<s:textfield name = "customerName"
label = "Customer Name"
value = "%{userInfo.customerName}"/>
Use OGNL (Object Graph Navigation Language) to get value in any kind of field of struts. If you will not get value please check setter and getter of the variable.
<s:textfield name="transValueChange" id="transValueChange"
value ="%{transValue}" theme="simple" maxLength="30"
onkeypress="return isNumberKey(event)">
</s:textfield>
OR
<s:select name="propCode" id="propCode" list="propClassMasMap" theme="simple"
value="%{propCode}" onchange="" cssClass="text">
</s:select>
I used this:
<table>
<s:label>User Name:</s:label>
<s:textfield name="user.userid" cssClass="tb5" type="text" placeholder="User Name" value="%{#session.userid}" disabled="true" />
</table>
The disabled attribute is not mandatory, I used it to disable the textfield that way the values will be retrieved from the DB and will be disable for the user to make changes on it.

calculate in struts 2 tag?

I have an iterate and i want to calculate sum of the values like this :
<s:iterator value="myValues" status="myStatus">
<s:property value="value" />
</s:iterator>
<s:property value="total.here" />
I want to show the sum of "value" in "total.here".
Sorry for my bad english.
Thank you very much.
Assuming myValues is an array or a list of integral values accessible from your action:
<s:set var="total" value="%{0}" />
<s:iterator value="myValues">
<s:set var="total" value="%{top + #attr.total}" />
</s:iterator>
<s:property value="%{'' + #attr.total}" />
Samuel_xL's answer is right. But, in general, if you can edit your action class, I'd advice to make the calculation there instead of doing it in jsp.

Resources