typeMismatcch sting to long binding exception - spring-mvc

My set up is Spring 4.25 and hibernate validator 4.3
I have a form that has a string , in the model its a Long. I ??.properties file with many typeMismatch = user friendly message or typeMismatch.amount=another user friendly message
why am I unable to pick up the 'user friendly message' instead I get a Failed to convert property value of type [java.lang.String] to required type [java.lang.Long] for property 'amount'; nested exception is java.lang.NumberFormatException: For input string: "dfg".
Do I need a custompropertyeditor ?
I can't make any sense?
Regards,
jemrug

Related

Conversion from string "valor" to type 'Integer' is not valid in VB.NET

I'm a C# programmer but now I have to code VB.NET and I having a situation trying to translate this line of C# code to VB.NET
what I want is to check if the value that I obtained via MySQLDataReader is DBNull, in C# I use this to check if a string value is null or not
data.empName = dr.IsDBNull(1) ? "No name !!!" : dr.GetString(1).ToString();
But now I need the same verification but in VB.NET and also the data type retrieved via MySQLDataReader is decimal, this is what I got for the time beign
IIf(dr.IsDBNull("valor"), 0.0, dr.GetDecimal(dr.GetOrdinal("valor")))
but when I debug the app got the error message:
An exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll but was not handled in user code
Additional information: Conversion from string "valor" to type 'Integer' is not valid.
the data type in the MySQL table is Decimal(10,2), could you please help me and tell me what am I doing wrong
The line should have read, the evaluation of dr.IsDBNull("valor") was incorrect and caused the exception. To remedy, try this
IIf(dr.IsDBNull(dr.GetOrdinal("valor")), 0.0, dr.GetDecimal(dr.GetOrdinal("valor")))

match datetime pattern with spring DatetimeFormat annotation

I'm trying to use this jquery input mask plugin to submit a date time field.
The input is being submitted as:
081220151530 - which is 3:30pm 08/12/2015
or in am/pm it's submitting
081220150330p
I'm trying to bind that value to a Joda DateTime field and I think I should be able to specify a custom pattern on the field annotation.
But this pattern's not matching and throwing a validation fault:
#org.springframework.format.annotation.DateTimeFormat(pattern = "DDMMYYYYhhmm")
private DateTime followUp;
The error:
Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.DateTime' for property 'followUp'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type #javax.persistence.Basic #org.hibernate.annotations.Type #org.springframework.format.annotation.DateTimeFormat org.joda.time.DateTime for value '081220151230'; nested exception is org.joda.time.IllegalFieldValueException: Cannot parse "081220151230": Value 22 for monthOfYear must be in the range [1,12]
So 2 questions:
1. Ideally, it could parse the am/pm value, so is there a pattern that will match the am/pm style?
If not, what's the right pattern string to match the 24 hour version?
Update to original question:
So I figured out the 24H pattern, using the DateFormat pattern ddMMyyyyHHmm
But the second part I can't get - so I'm going to reassign this to the jquery-inputmask tag because the fact that it's submitting the am/pm as a and p respectively maybe a bug....
So I think I can solve this if someone knows how to make it submit pm/am instead?
try: ddMMyyyyHHmma
post the value like 05052016

MVC Localization of Default Model Binder

I am currently trying to figure out how to localize the error messages generated by MVC. Let me use the default model binder as an example, so I can explain the problem.
Assuming I have a form, where a user enters thier age. The user then enters "ten" in to the form, but instead of getting the expected error of
"Age must be beween 18 and 25."
the message
"The value 'ten' is not valid for Age."
is displayed.
The entity's age property is defined below:
[Range(18, 25, ErrorMessageResourceType = typeof (Errors),
ErrorMessageResourceName = "Age", ErrorMessage = "Range_ErrorMessage")]
public int Age { get; set; }
After some digging, I notice that this error text comes from the System.Web.Mvc.Resources.DefaultModelBinder_ValueInvalid in the MvcResources.resx file.
Now, how can create localized versions of this file?
As A solution, for example, should I download MVC source and add MvcResources.en_GB.resx, MvcResources.fr_FR.resx, MvcResources.es_ES.resx and MvcResources.de_DE.resx, and then compile my own version of MVC.dll?
But I don't like this idea. Any one else know a better way?
See http://forums.asp.net/p/1512140/3608427.aspx, scroll down to Brad Wilson's reply near the bottom of that page (Sat, Jan 09 2010, 3:20 PM). There are static properties on the DefaultModelBinder that you can set to localize the generic error messages.
The reason a generic error message is used instead of your [Range] message is that [Range] provides a validation error message, but this particular case is a binding error. There's absolutely no way the framework can ever hope to convert the string "ten" to an Int32, so it can't even fire the [Range] validator. This is what the "PropertyValueInvalid" key as mentioned in that forum controls.
In MVC3 do the following to change default messages:
add the App_GlobalResources folder to your ASP.NET site
add a new resource file, call it f.ex. MyResources.resx
add these keys
PropertyValueRequired: A value is required.
PropertyValueInvalid: The value '{0}' is not valid for {1}.
in Application_Start of global.asax.cs add the line DefaultModelBinder.ResourceClassKey = "MyResources";
Have you tried: IDataErrorInfo property
This article will help
Validating with the IErrorDataInfo
Interface (C#)

getautosuggestdata type is not supported

I am using Google AutoSuggestData. It's implemented fine and working in a page
but when trying to implement in another page it gives error
system exception :getautosuggestdata
type is not supported
It looks like you forgot to include the DataType property when including the control in the new page. If you set a value that it doesn't recognise, you get an exception like "getautosuggestdata type 'Foo' is not supported", so I guess the error see is what will happen when the property is empty.

ognl.MethodFailedException "setCreatedDate" failed for object com.security.data.PasswordHistory#d5b561

i am using strut2.1.6 with tomcat 5.5
i have a Date field createDate in my PasswordHistory Bean, and corresponding date component on my "search.jsp" this field is optional - no validation required .
if i submit the form i am get the follwoing error on console
ognl.MethodFailedException: Method "setCreatedDate" failed for object com.security.data.PasswordHistory#d5b561 [java.lang.NoSuchMethodException: setCreatedDate([Ljava.lang.String;)] –
it looks that it is trying to convert the empty string to date, when it fails it tries to search the corresponding String argument method and if it converts the value to date successfully it calls the corresponding Date argument method – Muhammad Shahid
i want to avoid any conversion if the field is emtpy.
Do you have struts.devMode = true in struts.xml? From the docs:
And thirdly, perhaps the setting which is less widely known, and therefore a source of much confusion: it will raise the level of debug or normally ignorable problems to errors. For example: when you submit a field which cannot be set on an action 'someUnknownField', it will normally be ignored. However, when you're in development mode, an exception will be thrown, telling you an invalid field was submitted. This is very useful for debugging or testing large forms, but can also be confusing if you're relying on parameters in your request that are not set on the action, but which you are using directly in your view layer (warning: bad practice, you should always validate input from the web).
http://struts.apache.org/2.1.6/docs/devmode.html

Resources