Using SpringMVC + Thymeleaf, how does one bind an integer based model attribute, to an input field in a form using th:value or th:field, without having a value of '0' show in the field itself.
The issue is not with Thymeleaf, it is on the EL implementation of Tomcat. It doesn't respect the difference between the nullable Integer and primitive int. It is always coercing null values to 0. You can turn off this behavior using this VM argument :
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
or programatically by
System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");
If you choose the programatic way, make sure that you invoke it during initialization of ServletContext (ServletContextListener#contextInitialized)
Just remove th:field attribute and write id and name attributes with corresponding logical name.
Related
How to display a custom message or remove input for integer field in jsp form when user enters white-space in input-field in spring mvc, validated using hibernate validator?
It currently shows:
Failed to convert property value of type java.lang.String to required
type int for property freePasses; nested exception is
java.lang.NumberFormatException: For input string: ""
Use wrapper class to declare integer variables so that wrapper class will convert whitespace to null.
or
refer below link for common solution
Hibernate validation annotation - validate that at least one field is not null
I'm writing a REST service using Spring Boot and JPA. I need to be able to validate some of the input fields and I want to ensure I'm using a proper pattern for doing so.
Let's assume I have the following model and I also have no control over the model:
{
"company" : "ACME"
"record_id" : "ACME-123"
"pin" : "12345"
"company_name" : ""
"record_type" : 0
"acl" : ['View','Modify']
"language" : "E"
}
The things I need to do are:
Ensure the value is not empty - This seems simple enough using the #NotEmpty annotation and I can pass a message.
Ensure the value is part of a valid list of values - The example here is the language property in the model above. I want the value to be either E,F or S. This seems possible using a custom annotation (eg #ValidValue({"E","F","S"})) but is there a better/"Springy" way to do this?
Ensure the values in a list are part of a valid list of values - The example here is the acl property. Again this seems possible with a custom annotation like #ValidListValues({"View", "Modify", "Delete", "Hide"}) but same question as above.
Set a default value - From what I read, custom validator annotations are only able to validate and not modify. I would like to do something like #DefaultValue(value=5) if the value is null. Is this possible? More on this below.
Set a default value to the return of a static method - For example if the pin field in model above isn't set, I want to set it to something like Util.getRandomDigitsAsString(5).
Use values from another property - I would like to validate that one property contains the string from another property. Using the example model, I want to ensure that record_id starts with company.
I have this setup in what I believe is a standard way with the controller -> service -> DTO -> DAO -> Model. Another option I was thinking about was creating a method in the validateCreate() that would go through all of the items above and throw an exception if needed.
Thanks.
Yes, NotEmpty is the right way
You should define a Language enum. The language field of your POJO should be of type Language
Same as 2. Define an Acl enum.
Define that in your Java code. Initialize the value of the field to 5 by default. If the JSON contains a value, Jackson will set the field value to the value in the JSON. Otherwise, it will stay as 5. Or initialize the field to null, and add a method getValueOrDefault(int defaultValue) that returns the default value you want if the value is null.
Same as 4
Define a custom validator that applies on the class itself, rather than a property of the class. In the validator chec that the two related values are correct.
I use Hibernate Validation 4.3.1.Final and Spring 3.2.0.RELEASE
I have a form with a string attribute. This attribute is not required, but if it contains some value this value should be exactly 10 digits length.
So I need somthing like:
#NotRquired
#Length(min = 10, max = 10)
But there is no annotation like #NotRquired. How I should write validation for this case?
Not that a #NotRquired annotation would not help in this case, because all defined constraints are evaluated. So in the case where the value is not specified #Length would still be validated and fail. There are several things you could do:\
Use the #Pattern constraint and define a pattern which matters the empty string and the one of length 10 characters. Provided of course for not required case you get an empty string
Write a custom constraint, for example #EmptyOrLength which does what you want
Try working with validation groups and assign each constraint to a different group. You then need a way to target the right validation group
Revert to Hibernate Validator specific functionality and use boolean composition of constraints - http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#d0e3701. You still need a custom constraint though, but you can use a composition of #NotNull and #Length using #ConstraintComposition(OR)
I'm developing an application based on Spring Framerwork. As a view technology I use integrated with the framework Freemarker. Problems occur when java bean that stores data for vizualization have a null in some fields. There is no null conception in Freemarker so it considers that there is no these fields in the bean at all. I suppose problem could be solved by customization of class that copies data from the java bean to freemarker's hash object referred in template. But i haven't found what class does it in Spring. Is there such class and how is it called?
Usually you just deal with nulls directly in the template. E.g:
${person.surname!"n/a"}
which will print "n/a" in case of a null surname, or just:
${person.surname!}
which will print out the empty string (nothing) in case of a null surname.
You can use the "!" operator. Here is an example :
${your_property!""}
It will print the empty string "" if your_property is null.
If any part in your property chain can be null, you can also put parantheses around it to guard against any part being null. E.g. if you pu
${person.car.door.color!"<no value"}
you only guard against the color being null. But if it could also happen that the door, the car or the whole person is gone missing, you have to put
${(person.car.door.color)!"<no value"}
In a spring configuration, what is the difference between using name vs id? I'm aware that XML restricts the "id" attribute to be unique in a document and limits the characters for using in the id. But otherwise when declaring a bean, what is the difference between using the "name" attribute vs the "id" attribute?
Essentially, this is really just a XML matter. But you can also use the name attribute to specify aliases for a bean using characters which would be illegal in an id, I think.
In general, you should try to use id instead of name when you can. That way, the parser can catch duplicates for you.