How to handle String input for for an Integer Field
Get link
Facebook
X
Pinterest
Email
Other Apps
-
How to Make Integer field required and handle Strings? Question:
I am getting the following error when i submit the form with an empty value for customer "freePasses". I am using @NotNull on the field "freePasses". It is not throwing "is required" after validation after submit.
How to fix this? Please help.
Also, how do I handle validation if the user enters String input for the integer field?
----- Answer:
Great question!
The root cause is the freePasses field is using a primitive type: int. In order to check for null we must use the appropriate wrapper class: Integer.
To resolve this, In Customer.java, replace "int" with "Integer"
@NotNull(message="is required") @Min(value=0, message="must be greater than or equal to zero") @Max(value=10, message="must be less than or equal to 10") private Integer freePasses;
Then update your getter/setter methods to use "Integer"
public Integer getFreePasses() { return freePasses; }
public void setFreePasses(Integer freePasses) { this.freePasses = freePasses; }
Save everything and retest.
=====
Here is the full source code.
package com.luv2code.springdemo.mvc;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
public class Customer {
private String firstName;
@NotNull(message="is required")
@Size(min=1, message="is required")
private String lastName;
@NotNull(message="is required")
@Min(value=0, message="must be greater than or equal to zero")
@Max(value=10, message="must be less than or equal to 10")
private Integer freePasses;
@Pattern(regexp="^[a-zA-Z0-9]{5}", message="only 5 chars/digits")
private String postalCode;
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getFreePasses() {
return freePasses;
}
public void setFreePasses(Integer freePasses) {
this.freePasses = freePasses;
}
}
==== Handle String Input for Integer Fields
If the user enters String input such as "abcde" for the Free Passes integer field, we'd like to give a descriptive error message.
We basically need to override the default Spring MVC validation messages.
Follow these steps.
1. In your Eclipse project, expand the node: Java Resources
2. Right-click the src directory and create a new sub-directory: resources
3. Right-click the resources sub-directory and create a new file named: messages.properties
Your directory structure should look like this: 4. Add the following entry to the messages.properties file typeMismatch.customer.freePasses=Invalid number
5. Save file
---
This file contains key/value pairs for the error message type
For a basic example: typeMismatch.customer.freePasses=Invalid number
The format of the error key is: code + "." + object name + "." + field
To find out the given error code, in your Spring controller, you can log the details of the binding result System.out.println("Binding result: " + theBindingResult);
For details, see the docs here
- http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/DefaultMessageCodesResolver.html
---
6. Edit your config file: WEB-INF/spring-mvc-demo-servlet.xml
Add the following:
7. Save the file. Restart the Tomcat server
8. Run your app and add bad data for the "Free Passes" field. You will see the error message from our properties file.
History About php (Preprocessor HyperText) click here http://en.wikipedia.org/wiki/PHP php the right way is the best site for learn php and it will teach you about good programming technique visit here learn yourself best of luck www.phptherightway.com Spring Framework Question: How to use properties file to load country options Answer: This solution will show you how to place the country options in a properties file. The values will no longer be hard coded in the Java code. 1. Create a properties file to hold the countries. It will be a name value pair. Country code is name. Country name is the value. New text file: WEB-INF/countries.properties BR=Brazil FR=France CO=Colombia IN=India Note the location of the properties file is very important. It must be stored in WEB-INF/countries.propertie...
When performing Spring MVC validation, the location of the BindingResult parameter is very important. In the method signature, the BindingResult parameter must immediately after the model attribute . If you place it in any other location, Spring MVC validation will not work as desired. In fact, your validation rules will be ignored. @RequestMapping("/processForm") public String processForm( @Valid @ModelAttribute("customer") Customer theCustomer, BindingResult theBindingResult) { ... } Here is the relevant section from the Spring Reference Manual --- Defining @RequestMapping methods @RequestMapping handler methods have a flexible signature and can choose from a range of supported controller method arguments and return values. ... The Errors or BindingResult parameters have to follow the model object that is being bound immediately ... Source: https://docs....
Comments
Post a Comment