I am developing a small application on spring mvc and I want to put conditions in my input field "free passes" so that the user cannot add a number which is not between 0 and 10.
when i click on the validate button the following error is displayed:
org.springframework.beans.NotReadablePropertyException: Invalid property 'freePasses' of bean class [com.luv2code.springdemo.mvc.Customer]: Bean property 'freePasses' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622)
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:612)
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104)
at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228)
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:129)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:149)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:139)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:122)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:460)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:357)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:87)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:83)
at org.apache.jsp.WEB_002dINF.view.customer_002dform_jsp._jspx_meth_form_005finput_005f2(customer_002dform_jsp.java:374)
the problem is I really don't know where this error comes from.
this is my code of customer class :
package com.luv2code.springdemo.mvc;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Customer {
private String firstName;
@NotNull(message="is required !")
@Size(min=1, message="is required !")
private String lastName;
@Min(value=0, message="le champs doit avoir une valeur entre 0 et 10")
@Max(value=10, message="le champs doit avoir une valeur entre 0 et 10 !!!")
private int freePasses;
public int getFreePasses() {
return freePasses;
}
public void setFreePasses(int freePasses) {
this.freePasses = freePasses;
}
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;
}
}
and this is my code of customerController class :
package com.luv2code.springdemo.mvc;
import javax.validation.Valid;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
}
@RequestMapping("/showForm")
public String showForm(Model theModel) {
theModel.addAttribute("customer", new Customer());
return "customer-form";
}
@RequestMapping("/processForm")
public String processForm(@Valid @ModelAttribute("customer") Customer theCustomer, BindingResult theBindingResult) {
System.out.println("Last name : |" + theCustomer.getLastName()+ "|");
if(theBindingResult.hasErrors()) {
return "customer-form";
}
else {
return "customer-confirmation";
}
}
}
and this is the code of my customer-form.jsp :
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<title>Customer form</title>
<style type="text/css">
.error{color:red}
</style>
</head>
<body>
<form:form action="processForm" modelAttribute="customer">
First name: <form:input path="firstName"/>
<br><br>
Last name (*) : <form:input path="lastName"/>
<form:errors path="lastName" cssClass="error" />
<br><br>
Free passes : <form:input path="freePasses"/>
<form:errors path="freePasses" cssClass="error" />
<input type="submit" value="Valider" />
</form:form>
</body>
</html>
and this is my code of customer-confirmation.jsp :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>Customer Confirmation</title>
</head>
<body>
the customer is confirmed : ${customer.firstName} ${customer.lastName}
<br><br>
Free passes : ${customer.freePasses}
</body>
</html>
to better explain I want the user to be able to enter just a number between 0 to 10 in the free passes field and when he clicks on validated he displays the number he has entered in the input field "free passes"
Can someone help me to resolve this error please?