Thymeleaf - Exception evaluating SpringEL expression on object variable - thymeleaf

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.

Related

Binding a Set of domain object in Grails command object

I'm trying to bind a Set of objects to my Grails Command Objects, but currently, it doesn't work an throws errors:
Field error in object 'NewPersonCommand' on field 'addresses': rejected value [[Ljava.lang.String;#6d64b5fb]; codes [NewPersonCommand.addresses.typeMismatch.error,NewPersonCommand.addresses.typeMismatch,newPersonCommand.addresses.typeMismatch.error,newPersonCommand.addresses.typeMismatch,typeMismatch.NewPersonCommand.addresses,typeMismatch.addresses,typeMismatch.java.util.Set,typeMismatch]; arguments [addresses]; default message [Could not find matching constructor for: Address(java.lang.String)]>
I have the following command object in my Grails application:
class NewPersonCommand {
String name
Set<Address> addresses
}
And the code for the form I'm posting looks like this:
<input type="hidden" name="addresses" value="1" />
<input type="hidden" name="addresses" value="4" />
<input type="hidden" name="addresses" value="18" />
Any idea how I can fix this, so I can bind my set of addresses to the command object?

All radioButtons are false at the view

#Html.RadioButton("smth",true)
This row at the page looks like UNCHECKED radioButton. Why?
You should use
HtmlHelper.RadioButton(string name, object value, bool isChecked)
extension method.
First argument is the name of the form field which is input field generated by html helper.
Second argument is the value of input element. If this radio is selected when the postback to server happens, this value is used.
Third argument is what you are looking for. If it is true it makes radio button selected.
For instance,
#Html.RadioButton("Name", "Value" ,true)
would generate an input element which looks like following,
<input checked="checked" id="Name" name="Name" type="radio" value="Value" />
You need to use something of the form
#Html.RadioButton(id,value,checked (bool true/false))
So
#Html.RadioButton("A","B",true)
For example would produce:
<input checked="checked" id="A" name="A" type="radio" value="B" />
The documentation for this is here
In your previous query, you got this answer.
You asked the same query in it's comment section. The problem was that the second line of code was missing one parameter.
Please check below details....
Parameter Details
Razor Syntax
#Html.RadioButton("smth", "smth", true)
#Html.RadioButtonFor( m => m.Prop, true, new { id = "rdBtn" } )

Syntax error with parseJSON during unobtrusive validation

My MVC app is generating the following HTML which causes a Javascript syntax error upon submission (I'm not typing anything into the two text boxes). Here's the generated HTML and the submit handler:
<form action="/UrIntake/Save" id="UrIntakeForm" method="post">
<input data-val="true" data-val-length="The field LastName must be a string with a maximum length of 50." data-val-length-max="50" data-val-required="The LastName field is required." id="FormSubmitter_LastName" name="FormSubmitter.LastName" type="text" value="" />
<input data-val="true" data-val-length="The field FirstName must be a string with a maximum length of 50." data-val-length-max="50" data-val-required="The FirstName field is required." id="FormSubmitter_FirstName" name="FormSubmitter.FirstName" type="text" value="" />
<div id="SubmissionButtons" class="right">
<input type="button" onclick="SubmitForm()" value="Submit" />
<input type="button" onclick="CancelForm()" value="Cancel" />
</div>
</form>
function SubmitForm() {
$("#UrIntakeForm").valid();
.
.
.
This is the jQuery code where the syntax error is occurring (v1.9.0). "data" is undefined and the "return" line is where the error occurs:
parseJSON: function( data ) {
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
Presumably, I don't have to enter anything into the text boxes (and should then get the "field is required" message). Is this what's causing the error? That doesn't make sense, but I don't see what else it could be.
Cause
This is an issue with jquery.validate.unobtrusive.js in your ASP.NET.MVC package.
As of jQuery 1.9, the behavior of parseJSON() has changed and an undefined value would be considered a malformed JSON, resulting in the error you've specified. See the jQuery 1.9 Core Upgrade Guide for more information.
Solution
Use the jQuery Migrate plugin, which among other things adds backward-compatibility to the jQuery parseJSON() utility.
EDIT
According to the official announcement in this thread on Microsoft Connect, the issue has been resolved in the latest release of the framework.
Naturally, as Andreas Larsen noted in the comments, make sure to clear any relevant cache, server-side and client-side, after upgrading to the new release.
I also had this issue. The problem was that $.parseJSON(undefined) causes an exception to be thrown, and that the unobtrusive validation was making that call. As stated in the accepted answer, this has since been fixed.
You can download the Microsoft version of this script which will properly validate without causing an exception from this link: http://ajax.aspnetcdn.com/ajax/mvc/5.1/jquery.validate.unobtrusive.min.js

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');
});
});

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

Resources