Struts 2 ApplicationResources.properties error - struts2

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

Related

Thymeleaf - Exception evaluating SpringEL expression on object variable

I am sticking a value into a hidden input with Thymeleaf and keep getting an error that says Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "receiptInquirySearchForm.cardNumber?:''" (template: "results.html" - line 14, col 44)
I have tried putting the ? after receiptInquirySearchForm, after cardNumber, and after both. I keep getting that same error on that line.
Here is line 14:
<input type="hidden" name="cardNumber" data-th-value="${receiptInquirySearchForm.cardNumber?}" />
Now I know receiptInquirySearchForm is a valid non-null object because I have several other hidden inputs that do not throw errors.
<input type="hidden" name="tokenId" data-th-value="${receiptInquirySearchForm.tokenId}" />
<input type="hidden" name="accountNumber" data-th-value="${receiptInquirySearchForm.accountNumber}" />
<input type="hidden" name="sku" data-th-value="${receiptInquirySearchForm.sku}" />
When I change the data-th-value from cardNumber to tokenId, it gets past that block of hidden inputs so every other line works fine.
UPDATE
I found another more descriptive error message down below.
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'cardNumber' cannot be found on object of type '...web.form.ReceiptInquirySearchForm' - maybe not public or not valid?
How can I check for that in the code? I know sometimes it will be there, but apparently in this instance it is not.
They were doing this in Velocity like this:
<input type="hidden" name="cardNumber" value="$!receiptInquirySearchForm.cardNumber" />
The exclamation correctly handled the possible missing or null cardNumber.
Just as #Metroids pointed out, you are probably missing getters/setters for the field cardNumber especially a getter for it. If you have a getter for it, check that the getter follows the POJO convention and is public like so;
public int getCardNumber()
{
return cardNumber;
}
If the spelling is not like so getCardNumber(), even though you can call the method in the controller to get the value, thymeleaf cannot do so because it relies on POJO convention to be able to call variable properties. I hope this helps.

struts 2: equal symbol expected error

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>

Grails <g:if> in <g:select>

I have this <g:select> in a .gsp file. But unlike any ordinary <g:select>'s this one would have the attribute disabled="" if a certain condition is met.
Following the code:
<g:select name="test"
from="${["foo1","foo2"]}"
<g:if test="${true}">disabled=""</g:if> />
It returned an error: Grails tag [g:select] was not closed
But when I change it into this:
<g:select name="test"
from="${["mu1","mu2","mu3"]}"
${ if(true) { println "disabled=\"\"" } }/>
It returned this error: Attribute value must be quoted.
Both of the error message are under the exception, org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
The question is how could we make this work? Is there a possible answer without using a custom TagLib?
The GSP form field tags treat disabled as a boolean property, so you can say
<g:select .... disabled="${true}" />
Generally you should be able to use any expression under the usual Groovy-truth rules but I believe it makes a special case for the strings "true" and "false" (the latter would normally be considered true under Groovy-truth rules as a non-empty string). If in doubt you can always say
disabled="${(someExpression) as boolean}"
No need to use the println, try this
<g:select .... ${(conditional)?"disabled":""} ... />
<g:select disabled="${true}"...
is fine but when you submit and it is a required field the value will not be submitted so use this jQuery code to enable the field when pressing the submit button
$(function() {
$('form').on('submit', function() {
$(this).find(':disabled').removeAttr('disabled');
});
});

Change inputText value from listener method

I have an inputText:
<h:inputText id="result" value="#{guessNumber.result}"/>
and another inputText:
<h:inputText id="name" value="#{guessNumber.named}" onchange="submit()" valueChangeListener="#{guessNumber.processValueChange}"/>
and inside the processValueChange method, I added the following line:
result = "hello";
but the displayed value of "result" inputText remains unchainged, what is the problem?
The valueChangeListener method isn't intended to make changes to the model this way. It's intented to listen on the actual change of the model value, at exactly the moment where you have a hand of both the old and new model value. For example, to perform some logging. It runs at the end of the validations phase, right before the update model values phase. So any changes which you make to the model values yourself inside the listener method would be overridden during the update model values phase.
You need a <f:ajax listener> instead. This runs during the invoke action phase, which is after the update model values phase.
<h:outputText id="result" value="#{guessNumber.result}" />
<h:inputText id="name" value="#{guessNumber.named}">
<f:ajax listener="#{guessNumber.namedChanged}" render="result" />
</h:inputText>
(note that I've removed the onchange="submit()" JavaScript handler!)
with
public void namedChanged(AjaxBehaviorEvent event) {
result = "Hello, you entered " + named;
}
(the argument is optional; if you don't need it, just omit it)
See also:
When to use valueChangeListener or f:ajax listener?
I found the solution:
Instead of binding the value attribute of the inputText to a String property (result) inside the bean, I bound the inputText to UIInput property inside the bean:
<h:inputText id="result" binding="#{guessNumber.result}"/>
and in the bean:
private UIInput result = null;
this lets the listener method modify the inputText value directly, instead of modifying the property that is bound to the value attribute of the inputText:
result.setValue("Hello");
Thanks for every one tried to solve my problem.

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